silverlight应用:学Silverlight 2系列(35):升级Silverlight 2 Beta 1应用程序到Beta 2

  Silverlight 2 Beta 2发布的后在原来Beta 1基础的上有了很多改变本文整理总结下Silverlight 2 Beta1 和Beta 2的间变化以及升级Silverlight 2.0 Beta 1应用到Beta 2过程中可能会遇到些问题

  1.卸载原有和Beta 1有关切组件

  2.安装Beta 2相关组件

  3.在打开原有项目后会出现如下提示对话框提示你是否要升级到最近版本

  Control控件变化Control控件放在.Windows.dll集中  原来在.Windows.Controls.dll中Control控件在Beta 2中都放在了.Windows.dll中这意味着这些Control控件都已经在Runtime中如果在原来中有如下代码需要移除:

xmlns:my="clr-:.Windows.Controls;assembly=.Windows.Controls"  并且需要修改对应命名空间下Control控件声明如:

<my:Button x:Name="myButton"/>  在Beta 2中经过这样调整的后可以极大减小xap文件体积

  Tooltip变化  在Silverlight Beta 2中已经不允许直接使用Tooltip了只能通过ToolTipService来使用另外移除了ToolTipService中下面些公有属性和思路方法:

  BetweenShowDelayProperty

  InitialShowDelayProperty

  ShowDurationProperty

  GetBetweenShowDelay

  GetInitialShowDelay

  GetShowDuration

  SetBetweenShowDelay

  SetInitialShowDelay

  GetToolTip

  SetShowDuration

  Beta 1中ToolTipService实现大家可以参考Silverlight 2 Beta 1Control控件源代码现在修改后ToolTipService公开API非常简单如下代码所示:

public ToolTipService
{
  public readonly DependencyProperty ToolTipProperty;
  public void SetToolTip(DependencyObject element, object value);
}
  对于如何使用ToolTipService如下代码所示:

<Button ToolTipService.ToolTip="This is ToolTip text"/>
<Button Content="Button">
  <ToolTipService.ToolTip>
    <TextBlock Text="text"/>
 </ToolTipService.ToolTip>
</Button>
  或者

ToolTipService.SetToolTip(myElementNeedingAToolTip, TextBox);从Control控件中移除字体属性  在Beta 2中字体属性都放在了Control基类中所以如果你在自定义Control控件时定义了自己字体属性需要进行修改这些属性包括:

  FontFamily

  FontSize

  FontWeight

  FontStyle

  Foreground

  FontStretch

  现在如果在父元素中定义了它们子元素将会自动继承这些属性如:

<UserControl x:Class="TerryLee.SilverlightBeta2Example.Page"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Width="400" Height="300"
  FontSize="16"
  FontFamily="Trebuchet MS">
  <Grid x:Name="LayoutRoot" Background="White">
    ……
  </Grid>
</UserControl>
DatePicker和Calendar  DatePicker和CalendarControl控件变化如下所示:

  Calendar

  [Remove] bool AreDatesInPastSelectable

  [Remove] Style DayStyle

  [Remove] Style MonthStyle

  [Remove] DateTime SelectableDateStart

  [Remove] DateTime SelectableDateEnd

  DatePicker

  [Remove] bool AreDatesInPastSelectable

  [Remove] DateTime SelectableDateStart

  [Remove] DateTime SelectableDateEnd

  Calendar和DatePicker

  [Remove] bool IsEnabled {get; ;}

  [Remove] DateTime DisplayDateStart {get; ;}

  [Remove] DateTime DisplayDateEnd {get; ;}

  [Add] DateTimeRange DisplayRange {get; ;}

  ButtonBase类变化  在Beta 1中ButtonBase是个partial类:

public partial ButtonBase : ContentControl
{
  // ......
}
  而在Beta 2中它修改为了个abstract类:

public abstract ButtonBase : ContentControl
{
  // ......
}
  并且移除了ernal成员这些成员包括:

  OnIsEnabledChanged

  OnGotFocus

  OnIsPressedChanged

  IsFocused

  IsMouseOver

  IsMouseOverProperty

  IsFocusedProperty

  OnKeyDown

  OnKeyUp

  OnLostFocus

  OnMouseEnter

  OnMouseLeave

  OnMouseLeftButtonDown

  OnMouseLeftButtonUp

  OnMouseMove

  如果你曾经利用ButtonBase类开发过相关Control控件可能需要做些修改

  TextBoxControl控件变化  在Beta 2中TextBoxControl控件支持带换行文本卷动多行文字选择等功能都源于TextBoxControl控件内部模板改变Beta 1中TextBoxControl控件模板如下:

<ControlTemplate
 xmlns='http://schemas.microsoft.com/client/2007'
 xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  <Border x:Name='ELEMENT_Content'"
  BorderThickness='{TemplateBinding BorderThickness}'
  BorderBrush='{TemplateBinding BorderBrush}'
  Background='{TemplateBinding Background}'
  Padding='{TemplateBinding Padding}'/>
</ControlTemplate>
  Beta 2中TextBoxControl控件模板

<ControlTemplate
 xmlns='http://schemas.microsoft.com/client/2007'
 xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  <Grid x:Name='RootElement'>
    <Grid.Resources>
      <SolidColorBrush x:Key='Background' Color='#FF003255'/>
      <SolidColorBrush x:Key='BorderBrush' Color='#FF000000'/>
      <SolidColorBrush x:Key='AccentBrush' Color='#FFFFFFFF'/>
      <LinearGradientBrush x:Key='FocusedStrokeBrush'
    StartPo='0.5,0' EndPo='0.5,1'>
        <GradientStop Color='#B2FFFFFF' Off='0'/>
        <GradientStop Color='#51FFFFFF' Off='1'/>
        <GradientStop Color='#66FFFFFF' Off='0.325'/>
        <GradientStop Color='#1EFFFFFF' Off='0.325'/>
      </LinearGradientBrush>
      <Storyboard x:Key='Normal State'>
        <DoubleAnimation Storyboard.TargetName='FocusVisualElement'
     Storyboard.TargetProperty='Opacity' To='0' Duration='0:0:0.0'/>
      </Storyboard>
      <Storyboard x:Key='Focused State'>
        <DoubleAnimation Storyboard.TargetName='FocusVisualElement'
     Storyboard.TargetProperty='Opacity' To='1' Duration='0:0:0.0'/>
      </Storyboard>
    </Grid.Resources>
    <Rectangle StrokeThickness='.5' RadiusX='2'
   RadiusY='2' Fill='{TemplateBinding Background}'/>
    <Rectangle StrokeThickness='.5' RadiusX='2' RadiusY='2'
   Stroke='#FF003255'/>
    <Border x:Name='ContentElement' Padding='{TemplateBinding Padding}'/>
    <Grid x:Name='FocusVisualElement' Opacity='0' IsHitTestVisible='False'>
      <Rectangle RadiusX='1' RadiusY='1' Margin='2'
    Stroke='{StaticResource AccentBrush}' StrokeThickness='1'/>
      <Rectangle RadiusX='1' RadiusY='1'
    Stroke='{StaticResource Background}' StrokeThickness='2'/>
      <Rectangle RadiusX='1' RadiusY='1'
    Stroke='{StaticResource FocusedStrokeBrush}' StrokeThickness='2'/>
    </Grid>
  </Grid>
</ControlTemplate>
  如果你在Beta 1中定义了TextBoxStyle需要进行些修改如ELEMENT_Content现在变为了ContentElement

  Image和ImageBrush变化  Beta 2中移除了ExceptionRoutedEventHandler而采用泛型EventHandler来实现并且移除了DownloadProgress属性

  Beta 1:

// Image :
  public event ExceptionRoutedEventHandler ImageFailed;
  public double DownloadProgress { get; ;}
// ImageBrush :
  public event ExceptionRoutedEventHandler ImageFailed;
  public double DownloadProgress { get; ;}
  Beta 2:

// Image :
  public event EventHandler<ExceptionRoutedEventArgs> ImageFailed;
// ImageBrush :
  public event EventHandler<ExceptionRoutedEventArgs> ImageFailed;
Control加上了边框属性  在Beta 2中Control基类提供了两个边框属性BorderBrush和BorderThickness如果大家在Beta 1中自定义Control控件时定义了这两个属性现在需要将其移除并利用基类提供属性

public abstract Control : FrameworkElement
{
  public Brush BorderBrush { get; ; }
  public Thickness BorderThickness { get; ; }
}
ListBox变化  在Beta 2中ListBox移除了如下属性和事件:

  SelectedItemsProperty

  SelectionModeProperty

  SelectedItems

  SelectionMode

  OnGotFocus(RoutedEventArgs)

  OnItemContainerStyleChanged(Style, Style)

  OnKeyDown(KeyEventArgs)

  OnLostFocus(RoutedEventArgs)

  OnSelectedItemsChanged(IList, IList)

  OnIsSelectionActiveChanged(Boolean, Boolean)

  OnSelectedIndexChanged(Int32, Int32)

  OnSelectedItemChanged(Object, Object)

  OnSelectionChanged(SelectionChangedEventArgs)

  OnSelectionModeChanged(SelectionMode, SelectionMode)

  对于ListBoxItem移除了如下属性和事件:

  OnGotFocus(RoutedEventArgs)

  OnLostFocus(RoutedEventArgs)

  OnMouseEnter(MouseEventArgs)

  OnMouseLeave(MouseEventArgs)

  OnMouseLeftButtonDown(MouseButtonEventArgs)

  IsMouseOver

  同时移除了ListBox选择模式枚举类SelectionMode也就意味着在Beta 2中ListBox只能实现单选除非自行做扩展其实在Beta 1中SelectionMode也只有个值SingleBeta 2中ListBox提供了SelectedItem属性和SelectionChanged事件

  WatermarkedTextBoxControl控件  在Beta 1中加入Control控件WatermarkedTextBoxControl控件到了Beta 2中被移除所以如果你在Beta 1应用中使用了该Control控件需要用TextBox来替换不过好在我们已经有了Beta 1中WatermarkedTextBoxControl控件源码在Beta 2下重新实现个也不是件什么难事

  新TabControl  在Beta 2中添加了新TabControlControl控件但是该Control控件放在.Windows.Controls.Extended集中使用时需要添加命名空间

  DataGridControl控件  非常幸运是如果我们在Beta 1应用中使用了DataGridControl控件变化地方并不多而是新增了很多有用功能如实现排序等

  扩展Control控件  .Windows.Controls.Extended仍然保留其中包括了 4个Control控件在使用时候需要对这些Control控件导入命名空间

  Calendar

  DatePicker

  GridSplitter

  TabControl

  如:

<UserControl
  xmlns:my="clr-:.Windows.Controls;assembly=.Windows.Controls.Extended"
  x:Class="TerryLee.SilverlightBeta2Example.Page"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Width="400" Height="300"
  FontSize="16"
  FontFamily="Trebuchet MS">
  <Grid x:Name="LayoutRoot" Background="White">
    <my:DatePicker x:Name="myDatePicker"></my:DatePicker>
    <my:Calendar x:Name="myCalendar"></my:Calendar>
  </Grid>
</UserControl>
Visual State Manager  在Silverlight 2 Beta2中引进个新东西是"Visual State Manager(视觉状态管理器)" (VSM)该功能将极大地方便你建造交互性Control控件模板VSM引入了你可在Control控件模板中利用2个基本概念:"视觉状态(Visual States)" 和 "状态迁移(State Transitions)"例如象按钮这样Control控件为自己定义了多个视觉状态: "Normal(正常)", "MouseOver(鼠标的下)", "Pressed(按下)", "Disabled(不可用)", "Focused(获取焦点)", "Unfocused(不具焦点)"在Blend中模板编辑模式下设计师现在可以轻松地编辑按钮在每个特定状态下外观以及设置迁移规则来控制从个状态迁移到另个状态时动画效果应该运行时间然后在运行时Silverlight会动态地运行合适动画故事板来把Control控件从个状态平滑地过渡到另个状态(ScottGu)

  网络通信WCF通信  在Beta 2中WCF现在支持双工通信服务了但是仍然只支持basicHttpBinding绑定这将促成个非常干净编程模型允许服务器将消息“推送”到Silverlight客户端而不用开发人员手工轮询服务器端变动这个编程模型在多种场景中都非常有用包括即时消息/聊天应用象股票行情和交易这样监测/更新应用等

  WebClient和HttpWebRequest  Beta 2中允许使用WebClient上传数据以及允许我们在后台线程上WebClient或者HttpWebRequest在异常处理上也需要做些改动

  Beta 1

public void Download
{
  WebClient client = WebClient;
  client.DownloadStringCompleted DownloadStringCompletedEventHandler(
    client_DownloadStringCompleted);
  try
  {
    client.DownloadStringAsync( Uri("http://Contoso.com/myfile.txt"));
  }
  catch (SecurityException ex)
  {
    // handle synchronous exception
  }
}
void client_DownloadStringCompleted(
  object sender, DownloadStringCompletedEventArgs e)
{
  // Set the text content of a control to the result
  MyControl.Text = e.Result;
}
  Beta 2

public void Download
{
  WebClient client = WebClient;
  client.DownloadStringCompleted DownloadStringCompletedEventHandler(
    client_DownloadStringCompleted);
  client.DownloadStringAsync( Uri("http://Contoso.com/myfile.txt"));
}
void client_DownloadStringCompleted(
  object sender, DownloadStringCompletedEventArgs e)
{
  try
  {
    // Set the text content of a control to the result
    MyControl.Text = e.Result;
  }
  catch (SecurityException ex)
  {
    // handle asynchronous exception
  }
}
LINQ to JSON支持  Beta 2中提供了LINQ to JSON支持允许你在Silverlight应用中轻松地查询过滤并将JSON结果映射到.NET对象上这方便了对已经发布在网上现有AJAX端点和服务和操作

  跨域Sockets  Beta2现在允许跨域HTTP和Socketsnetworking支持(意味着你应用可以区别于当前应用下载自站点网站WebSite)

  Silverlight会查看目标服务器上是否存在这样XML策略文件这文件表明了是否允许跨域网络访问Silverlight支持个我们开发新XML策略文件格式以及Flash策略文件(意味着可为Flash所用现有网站WebSite不用费额外功夫就可在Silverlight中)

  浏览器集成HtmlPage变化  Beta 1中HtmlPage提供了UnregisterScriptableObject思路方法允许我们取消注册个可被脚本访问托管对象Beta 2中已经移除了该思路方法对于取消注册个可在脚本中创建对象类型思路方法UnregisterCreateableType仍然保留

  HtmlElement变化  Beta 1中可以使用HtmlElement.GetProperty和HtmlElement.GetAttribute思路方法来互相访问HTML DOMProperty或者AttributeBeta 2已经取消了这行为只能使用HtmlElement.GetProperty思路方法来访问HTML DOMPropery而只能使用HtmlElement.GetAttribute来访问HTML DOMAttribute

  隔离存储隔离存储空间大小  IsolatedStorage是Beta 1中提供非常有用新特性在Beta 2中存储大小增加到了1M;

  增加空间思路方法更名  Beta 2中隔离存储请求增加空间思路方法名由TryIncreaseQuotaTo变化为了IncreaseQuotaTo微软解释此举是该思路方法虽然加了Try但仍然没有使用Try Pattern所以移除比较合适

  Beta 1:

IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication;
storage.TryIncreaseQuotaTo(maxSize);
  Beta 2:

IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication;
storage.IncreaseQuotaTo(maxSize);
新增按Site存储  在Beta 1下隔离存储只能按照应用存储Beta 2中增加按照站点存储:

  Beta 2:

IsolatedStorageFile storage;
storage = IsolatedStorageFile.GetUserStoreForApplication;
storage = IsolatedStorageFile.GetUserStoreForSite;
应用配置更名  应用设置ApplicationSettings类更名为IsolatedStorageSettings且sealed类

  Beta 1:

ApplicationSettings tings = ApplicationSettings.Default;
tings.Add("myKey", myValue);
tings.Save;
  Beta 2

IsolatedStorageSettings tings = IsolatedStorageSettings.ApplicationSettings;
tings.Add("myKey", myValue);
tings.Save;
IsolatedStorageSettings tings = IsolatedStorageSettings.SiteSettings;
tings.Add("myKey", myValue);
tings.Save;
OpenFileDialog变化  Beta 1中OpenFileDialog.ShowDialog思路方法返回值为DialogResultBeta 2中返回变成了bool类型

  其它APISetValue思路方法  在Beta 2中SetValue思路方法只能设置依赖属性类型而不会像在Beta 1中自动进行转换如:

  Beta 1

myButton.SetValue(Canvas.LeftProperty, 100);  Beta 2

object left = 100;
myButton.SetValue(Canvas.LeftProperty, left);
  当然本文整理总结还只是其中部分更为详细变化大家可以参考MSDN:

  http://msdn.microsoft.com/en-us/library/cc645049(VS.95).aspx

  顺便说句:Silverlight 2 Beta 2中文版开发包已经发布以前使用中文Visual Studio 2008安装不上朋友这下可以放心了:)



  http://www.microsoft.com/downloads/details.aspx?displaylang=zh-cn&FamilyID=50a9ec01-267b-4521-b7d7-c0dba8866434



Tags:  silverlight.2.0 silverlight是什么 silverlight silverlight应用

延伸阅读

最新评论

发表评论