Silverlight 2的变化(Breaking Changes in Silverlight 2)
这一次翻译的文章标题使用的浅绿色,希望能帮助大家调节一下眼睛.同时翻译这篇文章希望能对大家将Silverlight1.1的应用程序移植到Silverlight2.0能有帮助!
这片Msdn文章中的目录顺序与内容顺序有一点不同所以我就将内容调整一下与目录相对应.这样感觉要好一点!
翻译上有什么不好之处请多指正.
谢谢关注!!
原文地址
Breaking Changes in Silverlight 2
这部分的主要是讨论Silverlight runtime和Silverlight tools在Microsoft Silverlight 1.1 Alpha September Refresh 和 MIX 2008 release之间的变化.在文章中我们将集中讨论在可能会引起你旧的Silverlight-based application错误或新的发布的版本中的新特性/新增加不同的表现部分.
(1)改进!移植旧版本的代码到新版本
(2)更新Silverlight.js
(3)模版的更换
(4)Error Messages可能不可用.
(5)2-D API的变化
(7)新功能支持HttpWebRequest/HttpWebResponse
(8)PresentationFrameworkCollection<T> 的变化
(9)Mouse 和 Keyboard API 的变化
(10)改变TextBlock 和 它的Helper Classes
(11)XML DTD API 的变化和默认XmlResolver
(12)XamlReader.Load() 需要xmlns
(13)托管的FontSource(downloader)签名被移出
(14)XmlResolver的变化
(15)Assembly 变化
(16)新做一个Web Service Client Proxies
(17)WCF JSON Services 可以需要SOAP Endpoints(Endpoints 翻译版本太多)
(18)在Visual Studio,使用"Add Service Reference"替换"Add Web Reference"
(19)加载事件签名的变化
(20)托管的Downloader被移出
(21)BrowserHttpWebRequest 被移出
(22)托管的MediaElement/Image.SetSource(downloader, partName)被移出
(24)SetValue的变化
(25)Miscellaneous API 的变化
(26)添加System.Net.Sockets Namespace
(1)改进!移植旧版本的代码到新版本
如果你要移植之前使用Microsoft Silverlight 1.1 Alpha September Refresh版创建的Silverlight应用程序或很容易来到Silverlight 2 Beta 1 (MIX 08) release,那下面就是推荐方式.
1.你要得到最新版Microsoft Silverlight 2 Tools for Visual Studio 2008.
2.你还能从SDK中得到这些工具或在线Getting Started.在安装Silverlight 2之前要确定你已经卸载旧的工具.
3.创建一个新的Silverilght应用程序.
4.拷贝所有你的code/XAML进入你的工程
5.Debug.有很多改变,这些改变你都可以在文档被找到.
注意:这些需要与assembly names/identities---Microsoft assemblies的identities已经有下面的改变:
--public key已经改变为一个新的Microsoft Silverlight key.
--版本号(assembly and file version)已经改变为2.0.
因此你将需要更新您的assembly name/identity的使用.
(2)更新Silverlight.js
Silverlight.js文件已经改变并且你需要为你的应用程序更新.Silverlight.js旧的版本不能确保在客户端已经安装Silverlight 2 Beta 1.如果你编译你的Silverlight应用程序的Silverlight.js文件会被自动更新.
(3)模版的更换
这里是关于更换Silverlight Visual Studio templates的讨论.如果你正在使用Visual Studio创建Silverlight应用程序,请确定你使用最新版本Microsoft Silverlight 2 Tools for Visual Studio 2008来创建应用程序.如果这样做,你将不用担心有关于新工程的改变问题.但是如果你的现有应用程序已经使用旧的版本的VS Silverlight tools,你既可以使用最新的工具编译你的应用程序也可以自己动手变更你的工程.为人们在之后的遇到异常情形的改变概要在下面的部分:
1.添加SilverlightApplication 属性.这个允许这个工程系统中区别Silverlight applications和Silverlight class libraries.
New |
<SilverlightApplication>true</SilverlightApplication> |
Before/After |
<ZipOutputs> has been changed to <XapOutputs> <ZipFilename> has been changed to <XapFilename> |
现有工程文件,你可能手动编辑更改这些变化.
(4)Error Messages 可能不可用.
Exception 信息mscorlib.dll不再装载在Silverlight runtime中.取代的是,他们装载在Silverlight Software Development Kit (SDK)中.这变化允许Silverlight runtime变得更为小.因为它将不再保证异常信息到达客户端,Silverlight开发者从不将异常信息在显示在客户端.
开发者依然能像过去一样继续使用异常信息(捕获他们的try/catch的阻塞),但没有保证异常信息到达客户端.如果资源不能调试,就会有一个泛化的错误显示:"An error occurred.".
(5)2-D API的变化
从Point结构中删除:
#
static bool Equals (Point point1, Point point2)
#
override Equals (object o)
#
override Equals (Point value)
#
override int GetHashCode()
#
void Offset(double offsetX, double offsetY)
#
static explicit operator Size(Point point)
从Color结构中删除:
#
static Color FromScRgb(float a, float r, float g, float b)
#
static Color FromRgb(byte r, byte g, byte b)
#
static Color operator -(Color color1, Color color2)
#
static Color Subtract(Color color1, Color color2)
#
override string ToString()
#
static bool AreClose(Color color1, Color color2)
#
void Clamp()
#
static bool Equals(Color color1, Color color2)
#
bool Equals(Color color)
#
float ScA
#
float ScR
#
float ScG
#
float ScB
#
float a, r, g, b;
#
byte a, r, g, b;
从Size结构中删除:
#
static Size Empty { get; }
#
bool IsEmpty { get; }
#
bool Equals (Size value);
#
override int GetHashCode ();
#
static explicit operator Point (Size size);
从Matrix结构中删除:
#
static bool Equals (Matrix matrix1, Matrix matrix2);
#
bool Equals (Matrix value);
#
Vector Transform (Vector vector);
从Rect结构中删除:
#
override string ToString()
#
Rect(Point location, Size size);
#
Rect(Size size);
#
Point Location { get; set; }
#
Size Size { get; set; }
#
double Left { get; }
#
double Top { get; }
#
double Right { get; }
#
double Bottom { get; }
#
Point TopLeft { get; }
#
Point TopRight { get; }
#
Point BottomLeft { get; }
#
Point BottomRight { get; }
#
bool Contains(double x, double y);
#
bool Contains(Rect rect);
#
bool IntersectsWith(Rect rect);
#
static Rect Intersect(Rect rect1, Rect rect2);
#
static Rect Union(Rect rect1, Rect rect2);
#
static Rect Union(Rect rect, Point point);
#
void Offset(double offsetX, double offsetY);
#
static Rect Offset(Rect rect, double offsetX, double offsetY);
#
void Inflate(Size size);
#
void Inflate(double width, double height);
#
static Rect Inflate(Rect rect, Size size);
#
static Rect Inflate(Rect rect, double width, double height);
#
void Scale(double scaleX, double scaleY);
#
static bool Equals (Rect rect1, Rect rect2);
(7)新功能支持HttpWebRequest/HttpWebResponse
1.支持HTTP/HTTPS 请求
2.支持Web Services场景中的min-bar
a.GET/POST 方法
b.关于GET/POST请求:设置标准headers没有暗示.
c.关于GET/POST请求:设置自定义 headers
d.关于响应:读取内容类型
3.关于响应:得到OK或NotFound error代码
4.为可靠的网络特性平衡浏览器堆(Leverage browser stack for certain networking features)
a.Basic, digest, ntlm, & cookie基于身份验证的场景
b.代理探测,数据压缩,缓存.
5.只支持同步请求且只支持他们在UI线程下.
(8)PresentationFrameworkCollection<T> 的变化
所以当你使用Add(object)后放进IList中时PresentationFrameworkCollection<T> 现在使用隐式接口实现来实现IList,
Before |
collection.Add(myobject);
|
Now |
collection.Add ( (GradientStop) myobject);
or IList list = (IList) collection; list.Add(myobject); |
(9)Mouse 和 Keyboard API 的变化
你现在使用Key 枚举(KeyEventArgs.Key property)代替一个整数类型
KeyboardEventHandler/Args 现在是 KeyEventHandler/Args
MouseLeftButtonDown and MouseLeftButtonUp时间现在的类型是MouseButtonEventHandler/Args(其他鼠标事件保留,没有变化).
访问修饰符键(键盘的键)的状态(args.Shift/Control,等)现在是通过使用static Keyboard.Modifiers API,并且能在键盘事件外使用.
MouseEventArgs.Ctrl和MouseEventArgs.Shift 已经被删除.
Cursors 枚举已经被转变成一个Cursors类并且一个static Cursors类被多个cursors(arrow,Hand)填充,这里特别是Canvas.Cursor=Cursors.Hand将继续被使用.Cursors.Default已经被替换为null.但它的XAML保留没有改变,所以Cursor="Default"继续工作在XAML中.
这是一个示例:
之前
canvas.MouseLeftButtonUp += new MouseEventHandler(onClick);
void onClick (object sender, MouseEventArgs e) {
Canvas cc = sender as Canvas;
SolidColorBrush sb = new SolidColorBrush();
sb.Color = Colors.Red;
cc.Background = sb;
}
Canvas.KeyUp += new KeyboardEventHandler(onKeyUp);
Void onKeyUp(object sender, KeyboardEventArgs e){
if (e.Key == 12) return;
bool isShiftPressed = e.Shift;
if (isShiftPressed) {
canvas.Cursor = Cursors.Default;
}
void onClick (object sender, MouseEventArgs e) {
Canvas cc = sender as Canvas;
SolidColorBrush sb = new SolidColorBrush();
sb.Color = Colors.Red;
cc.Background = sb;
}
Canvas.KeyUp += new KeyboardEventHandler(onKeyUp);
Void onKeyUp(object sender, KeyboardEventArgs e){
if (e.Key == 12) return;
bool isShiftPressed = e.Shift;
if (isShiftPressed) {
canvas.Cursor = Cursors.Default;
}
现在
canvas.MouseLeftButtonDown += new MouseButtonEventHandler(onClick);
void OnClick(object sender, MouseButtonEventArgs e) {
Canvas cc = sender as Canvas;
SolidColorBrush sb = new SolidColorBrush();
sb.Color = Colors.Red;
cc.Background = sb;
}
Canvas.KeyUp += new KeyEventHandler(onKeyUp);
Void onKeyUp(object sender, KeyEventArgs e){
if (e.Key == Key.A) return;
Bool isShiftPressed =
( ( Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift);
if (isShiftPressed) {
canvas.Cursor = null;
}
}
void OnClick(object sender, MouseButtonEventArgs e) {
Canvas cc = sender as Canvas;
SolidColorBrush sb = new SolidColorBrush();
sb.Color = Colors.Red;
cc.Background = sb;
}
Canvas.KeyUp += new KeyEventHandler(onKeyUp);
Void onKeyUp(object sender, KeyEventArgs e){
if (e.Key == Key.A) return;
Bool isShiftPressed =
( ( Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift);
if (isShiftPressed) {
canvas.Cursor = null;
}
}
(10)改变TextBlock 和 它的Helper Classes
TextBlock.FontFamily属性已经从字符串类型转换成FontFamily类型.
之前
textblock.FontFamily = "Arial";
现在textblock.FontFamily = new FontFamily("Arial");
其他的变化:
FontWeights枚举已经已经转入FontWeight类并且一个static FontWeights类由多个weights填充,这里特别的是textblock.FontWeight = FontWeights.Bold 仍然继续使用.
FontStyles枚举已经已经转入FontStyle类并且一个static FontStyles类由多个Styles填充,这里特别的是textblock.FontStyle = FontStyles.Italic仍然继续使用.
FontStretches枚举已经已经转入FontStretche类并且一个static FontStretches类由多个Stretches填充,这里特别的是textblock.FontStretch = FontStretches.Expanded 仍然继续使用.
TextDecorations枚举已经已经转入TextDecoration类并且一个static FontStretches类由多个TextDecorations填充,这里特别的是textblock.TextDecoration = TextDecorations.Underline 仍然继续使用.
(11)XML DTD API 的变化和默认的XmlResolver
改变操作XML的API引入支持DTD分解功能更好的支持新的XmlXapResolver和XmlPreloadedResolver.这样的改变将会进入所有受权支持DTD支持XmlReader的代码都被.除这些外,XmlXapResolver已经添加像默认的为XmlReader样的resolver.
API 的改变
下面是当一个被加载Xml文件内容中有DTD时,API在操做DTD允许DTD被忽视的情况.
1.添加XmlResolver 如果Uri能用type参数的实例来表示那public virtual bool SupportsType(Uri uri, Type type)这个方法返回类型为true.只有为Stream,基类的实现返回值才为true.
2.添加DtdProcessing enumeration 当XmlReader遇到一个DTD时DtdProcessing enumeration允许展开动作.这个枚举有下面值:Parse, Ignore,和Prohibit.
3.删除XmlReaderSettings.ProhibitDTD属性 这个属性是DtdProcessing属性的补充.
4.添加XmlReaderSettings.DtdProcessing属性 当DtdProcessing property 已经添加遇到DTD时允许有相应动作.默认值为Prohibit时与变化之前有同样的行为.
之前
XmlReaderSettings settings = new XmlReaderSettings();
settings.XmlResolver = resolver;settings.ProhibitDtd = false;XmlReader reader = XmlReader.Create("fileInXap", settings);
现在settings.XmlResolver = resolver;settings.ProhibitDtd = false;XmlReader reader = XmlReader.Create("fileInXap", settings);
XmlReaderSettings settings = new XmlReaderSettings();
settings.XmlResolver = resolver;settings.DtdProcessing = DtdProcessing.Parse;XmlReader reader = XmlReader.Create("fileInXap", settings);
or
XmlReaderSettings settings = new XmlReaderSettings();
settings.XmlResolver = resolver;settings.DtdProcessing = DtdProcessing.Ignore;XmlReader reader = XmlReader.Create("fileInXap", settings);
settings.XmlResolver = resolver;settings.DtdProcessing = DtdProcessing.Parse;XmlReader reader = XmlReader.Create("fileInXap", settings);
or
XmlReaderSettings settings = new XmlReaderSettings();
settings.XmlResolver = resolver;settings.DtdProcessing = DtdProcessing.Ignore;XmlReader reader = XmlReader.Create("fileInXap", settings);
(12)XamlReader.Load() 需要xmlns
XamlReader.Load(“<Canvas/>”);
现在XamlReader.Load("<Canvas xmlns="http://schemas.microsoft.com/client/2007"/>");
为按照XML规则运行,XamlReader.Load()现在需要你在你的XAML文件中指定一个xmlns.
(13)托管的FontSource(downloader)签名被移出
这个构造器在托管代码中获得的是一个downloader被删除.现有的FontSource构造器获得的是一个流.
你应该选择使用WebClient类下载font和使用WebClient提供的stream.
TextBlock tb = this.FindName("tb");
tb.FontSource = new FontSource(myDownloader);
void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
TextBlock tb = this.FindName("tb");
tb.FontSource = new FontSource(e.Result);
}
(14)XmlResolver的变化
默认的XmlResolver现在是XmlXapResolver,能从应用程序的XAP中加载文件.允许从XAP中加载一个Xml文件不需要创建一个XmlReaderStrings实例.
XmlReaderSettings settings = new XmlReaderSettings();
settings.XmlResolver = resolver;XmlReader reader = XmlReader.Create("fileInXap", settings);
orXmlReaderSettings settings = new XmlReaderSettings();
settings.XmlResolver = resolver;
XmlReader reader = XmlReader.Create("fileInXap", settings);
XDocument doc = XDocument.Load(reader);
现在settings.XmlResolver = resolver;XmlReader reader = XmlReader.Create("fileInXap", settings);
orXmlReaderSettings settings = new XmlReaderSettings();
settings.XmlResolver = resolver;
XmlReader reader = XmlReader.Create("fileInXap", settings);
XDocument doc = XDocument.Load(reader);
XmlReader reader = XmlReader.Create("fileInXap");orXDocument doc = XDocument.Load("fileInXap");
还有这个变化是,
1.XmlResolver已经添加
2.XmlXapResolver:当引用一个Xml文件时这个resolver允许从一个XAP文件加载内容.
XmlPreloadedResolver:这个Resolver包括XHTML和RSS DTDs,更好能添加另外的DTDs进入预先加载到缓存.
(15)Assembly 变化
agclr.dll被重命名为System.Window.dll.
System.Xml.Core.dll被重命名为 System.Xml.dll .
System.Silverlight.dll 被重命名为 System.Windows.Browser.dll.
(16)新做一个Web Service Client Proxies
给用户的一个为Silverlight 1.1 Alpha和Alpha Refresh projects产生的Web service client proxies将重新产生一个为Silverlight的最新版本的proxies.因为System.Windows.Browser.Net.SoapHttpClientProtocol类被ClientBase<T>取代,这个类是在System.ServiceModel.dll中.
(17)WCF JSON Services 可以需要SOAP Endpoints(Endpoints 翻译版本太多)
用户依赖于为他们的Silverlight1.1 Alpha和Alpha Refresh 工程的WCF ASP.NET AJAX("WCF JSON") services,在最新版本的Silverlight版本中将添加SOAP终端.
用户依赖于为他们的Silverlight1.1 Alpha和Alpha Refresh 工程的".asmx" ASP.NET AJAX("ASMX JSON") services将不能使用,在大部分案例中,不得不从根本改变他们的服务端(除非SOAP被明确指定为disabled,在这些案例中它应该re-enabled).
(18)在Visual Studio,使用"Add Service Reference"被"Add Web Reference"取代
用户将需要为Silverlight使用服务将Add Service Reference替换Add Web Reference.
(19)Loaded事件签名的变化
Loaded事件签名现在是RoutedEventHandler/Args.它行为是没有改变(这里不同的是tunneling/bubbling.)RoutedEventArgs 和 RoutedEventHandler的namespace从System.Windows.Input 改变到 System.Windows.
之后
using System.Windows.Input;
canvas.Loaded +=
new EventHandler(My_Handler);
void My_Handler (object sender, EventArgs e) {
}
现在canvas.Loaded +=
new EventHandler(My_Handler);
void My_Handler (object sender, EventArgs e) {
}
using System.Windows;
canvas.Loaded +=
new RoutedEventHandler(My_Handler);
void My_Handler (object sender, RoutedEventArgs e) {
}
canvas.Loaded +=
new RoutedEventHandler(My_Handler);
void My_Handler (object sender, RoutedEventArgs e) {
}
(20)托管的Downloader被移出
托管的Downloader对象已经被移出.它的功能被WebClient类替换.不再有JavaScript Downloader对象.
之前
Downloader downloader = new Downloader();
downloader.Open("GET", "http://data.com/resource.xml");
downloader.DownloadProgressChanged += new EventHandler(downloader_DownloadProgressChanged);
downloader.Completed += new EventHandler(downloader_Completed);
downloader.DownloadFailed += new ErrorEventHandler(downloader_DownloadFailed);
downloader.Send();
void downloader_Completed(object sender, EventArgs e)
{
Downloader downloader = (Downloader)sender;
Stream stream = downloader.GetResponseStream();
}
现在downloader.Open("GET", "http://data.com/resource.xml");
downloader.DownloadProgressChanged += new EventHandler(downloader_DownloadProgressChanged);
downloader.Completed += new EventHandler(downloader_Completed);
downloader.DownloadFailed += new ErrorEventHandler(downloader_DownloadFailed);
downloader.Send();
void downloader_Completed(object sender, EventArgs e)
{
Downloader downloader = (Downloader)sender;
Stream stream = downloader.GetResponseStream();
}
WebClient webClient = new WebClient();
webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(webClient_DownloadProgressChanged);
webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);
webClient.DownloadStringAsync(new Uri("http://data.com/resource.xml"));
void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
if ((e.Error == null) && (e.Cancelled == false))
{
Stream stream = e.Result;
}
}
webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(webClient_DownloadProgressChanged);
webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);
webClient.DownloadStringAsync(new Uri("http://data.com/resource.xml"));
void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
if ((e.Error == null) && (e.Cancelled == false))
{
Stream stream = e.Result;
}
}
(21)BrowserHttpWebRequest 被移出
一个新的HttpWebRequest类被添加,并且BrowserHttpWebRequest类/实现已经被移出.
HttpWebRequest request = new BrowserHttpWebRequest(new Uri("http://Contoso.com"));
HttpWebResponse response = request.GetResponse();
Stream content = response.GetResponseStream();
using (XmlReader reader = XmlReader.Create(new StreamReader(content)))
{
while (reader.Read())
{
if ((reader.IsStartElement()) && ("item" == reader.LocalName))
{
using (XmlReader itemReader = reader.ReadSubtree())
{
while (itemReader.Read())
{
if (itemReader.IsStartElement())
{
if ("title" == itemReader.LocalName)
{
}
else if ("link" == itemReader.LocalName)
{
}
}
}
}
}
}
}
response.Close();
现在HttpWebResponse response = request.GetResponse();
Stream content = response.GetResponseStream();
using (XmlReader reader = XmlReader.Create(new StreamReader(content)))
{
while (reader.Read())
{
if ((reader.IsStartElement()) && ("item" == reader.LocalName))
{
using (XmlReader itemReader = reader.ReadSubtree())
{
while (itemReader.Read())
{
if (itemReader.IsStartElement())
{
if ("title" == itemReader.LocalName)
{
}
else if ("link" == itemReader.LocalName)
{
}
}
}
}
}
}
}
response.Close();
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(new Uri("http://contoso.com"));
request.BeginGetResponse(new AsyncCallback(ResponseCallback), request);
// AsyncCallback called back on UIThread
private void ResponseCallback(IAsyncResult asyncResult)
{
HttpWebRequest request = (HttpWebRequest)asyncResult.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asyncResult);
Stream content = response.GetResponseStream();
using (XmlReader reader = XmlReader.Create(new StreamReader(content)))
{
while (reader.Read())
{
if ((reader.IsStartElement()) && ("item" == reader.LocalName))
{
using (XmlReader itemReader = reader.ReadSubtree())
{
while (itemReader.Read())
{
if (itemReader.IsStartElement())
{
if ("title" == itemReader.LocalName)
{
}
else if ("link" == itemReader.LocalName)
{
}
}
}
}
}
}
}
content.Close();
}
request.BeginGetResponse(new AsyncCallback(ResponseCallback), request);
// AsyncCallback called back on UIThread
private void ResponseCallback(IAsyncResult asyncResult)
{
HttpWebRequest request = (HttpWebRequest)asyncResult.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asyncResult);
Stream content = response.GetResponseStream();
using (XmlReader reader = XmlReader.Create(new StreamReader(content)))
{
while (reader.Read())
{
if ((reader.IsStartElement()) && ("item" == reader.LocalName))
{
using (XmlReader itemReader = reader.ReadSubtree())
{
while (itemReader.Read())
{
if (itemReader.IsStartElement())
{
if ("title" == itemReader.LocalName)
{
}
else if ("link" == itemReader.LocalName)
{
}
}
}
}
}
}
}
content.Close();
}
(22)托管的MediaElement/Image.SetSource(downloader, partName)被移出
Image.SetSource(DependencyObject downloader, string partName)和MediaElement.SetSource(DependencyObject downloader, string partName)方法已经被移出.为得到一个从一个zip流中夺取的 Part,你应该使用WebClient的GetResourceStream.看下面:
Downloader downloader = new Downloader();
downloader.Open("GET", "http://foo.com/myimages.zip");
downloader.Completed += new EventHandler(downloader_Completed);
downloader.Send();
void downloader_Completed(object sender, EventArgs e)
{
MediaElement mediaElement = new MediaElement();
Downloader downloader = (Downloader)sender;
mediaElement.SetSource(downloader, "image1.jpg");
}
现在downloader.Open("GET", "http://foo.com/myimages.zip");
downloader.Completed += new EventHandler(downloader_Completed);
downloader.Send();
void downloader_Completed(object sender, EventArgs e)
{
MediaElement mediaElement = new MediaElement();
Downloader downloader = (Downloader)sender;
mediaElement.SetSource(downloader, "image1.jpg");
}
WebClient webClient = new WebClient();
webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);
webClient.OpenReadAsync(new Uri("http://foo.com/myimages.zip"));
void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
MediaElement mediaElement = new MediaElement();
if ((e.Error == null) && (e.Cancelled == false))
{
StreamResourceInfo zipResourceInfo = new StreamResourceInfo(e.Result, null);
StreamResourceInfo imageResourceInfo = Application.GetResourceStream(zipResourceInfo, new Uri("image1.jpg"));
mediaElement.SetSource(imageResourceInfo.Stream);
}
}
webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);
webClient.OpenReadAsync(new Uri("http://foo.com/myimages.zip"));
void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
MediaElement mediaElement = new MediaElement();
if ((e.Error == null) && (e.Cancelled == false))
{
StreamResourceInfo zipResourceInfo = new StreamResourceInfo(e.Result, null);
StreamResourceInfo imageResourceInfo = Application.GetResourceStream(zipResourceInfo, new Uri("image1.jpg"));
mediaElement.SetSource(imageResourceInfo.Stream);
}
}
(24)SetValue和GetValue的变化
DependencyObject.SetValue不再是泛化.
this.ClipName.SetValue<double>(TextBlock.FontSizeProperty, ((double) this.ClipName.GetValue(TextBlock.FontSizeProperty)) - 1);
this.ClipName.SetValue<string>(TextBlock.ForegroundProperty, "#FF2F2F2F");
this.ClipDate.SetValue<string>(TextBlock.ForegroundProperty, "#FF2F2F2F");
this.ClipLength.SetValue<string>(TextBlock.ForegroundProperty, "#FF2F2F2F");
现在this.ClipName.SetValue<string>(TextBlock.ForegroundProperty, "#FF2F2F2F");
this.ClipDate.SetValue<string>(TextBlock.ForegroundProperty, "#FF2F2F2F");
this.ClipLength.SetValue<string>(TextBlock.ForegroundProperty, "#FF2F2F2F");
this.ClipName.SetValue(TextBlock.FontSizeProperty, ((double) this.ClipName.GetValue(TextBlock.FontSizeProperty)) - 1);
this.ClipName.SetValue(TextBlock.ForegroundProperty, "#FF2F2F2F");
this.ClipDate.SetValue (TextBlock.ForegroundProperty, "#FF2F2F2F");
this.ClipLength.SetValue (TextBlock.ForegroundProperty, "#FF2F2F2F");
this.ClipName.SetValue(TextBlock.ForegroundProperty, "#FF2F2F2F");
this.ClipDate.SetValue (TextBlock.ForegroundProperty, "#FF2F2F2F");
this.ClipLength.SetValue (TextBlock.ForegroundProperty, "#FF2F2F2F");
当textblock.FontFamily返回一个FontFamily对象时textblock.GetValue(FontFamilyProperty)使用返回值一个字符串.上面GetValue行为已经有改变它返回的是一个FontFamily对象.类似的,FontWeight, FontStyle, FontStretch和TextDecorations也同样的一个方法.
if (textblock.GetValue(FontStyleProperty) == 3)
现在if (textblock.GetValue(FontStyleProperty) == FontStyles.Italic)
(25)Miscellaneous API 的变化
ArcSegment.Size 方法是从Point到Size.
LoadContent方法将从FrameworkTemplate到Data Template
添加一个TileMode枚举进入System.Windows.Media,并且修改TileBrush.TileMode使用它替换double.
下面的功能已经从HtmlPage中移到HtmlWindow:CurrentBookmark, Alert/Confirm/Eval/Prompt, CreateInstance, Navigate*/NavigateToBookmark.
下面功能从HtmlPage中移动到HtmlDocument中:Submit, Cookies, QueryString, DocumentUri.
Scriptable attribute已经分为两个attribute ScriptableTypeAttribute和ScriptableMemberAttribute.开发者当前的类使用Scriptable的只需要该为ScriptableTypeAttribute.开发者当前类的成员使用的是Scriptable,只需要将改为ScriptableMemberAttribute.
XmlUrlResolver被移出.
OpenFileDialog.Title被移出.
System.Windows.WebApplication被移出.开发者应该被一个新的System.Windows.Application类型替换.
UIElement::GetRenderSize被固定(返回Int32,和是绝对的访问器),是read-only,并且依赖的属性是私有的.
下面类型由System.Silverlight.dll移动到System.Windows.dll中.这些改变只需要为你的assemblies移到新的assembly位置重新编译:
System.Windows.Controls.DialogResult
System.Windows.Controls.FileDialogFileInfo
System.Windows.Controls.OpenFileDialog
System.Windows.Browser.Net.XBrowserHttpWebRequest和
System.Windows.Browser.Net.XBrowserHttpWebResponse是产生在System.Windows.dll的内部将不再为公共的使用.
XamlParseException.LineNumber和LinePostion属性的类型都将Int32该为UInt32.
委托 LayoutUpdatedEventHandler被移出,并且多有的引用被EventHandler所取代
LayoutInformation被从System.Windows.Contorls移动到System.Windows.Controls.Primitives
DownloadProgressEventArgs.Progress现在返回类型由Int32替换原来的double.
System.TimeZoneInfo 已经被移动到System.Core.dll中.
TimelineCollection 已经从System.Windows.Media中移动到System.Windows.Media.Animation
来于System.Windows.Input.Key enumeration中的几个enumerators被改成匹配WPF的命名.
StyleSimulations 从System.Windows中移到System.Windows.Media
TriggerCollection和EventTrigger 现在被托管代码产生.
DownloadProgressEventArgs 从System.Windows.MediaSystem中移到Windows.Media.Imaging.
Orientation 从 System.Windows 移动到 System.Windows.Controls
Popup 从 System.Windows.Controls 移动到 System.Windows.Controls.Primitives
Triggers 从 managed UIElement 移动到 FrameworkElement
PathFigure.Segments 是不再只有parser能写入
PathGeometry.Figures 是不再只有parser能写入
GeometryGroup.Children 是不再只有parser能写入
GradientBrush.GradientStops 是不再只有parser能写入
XmlReader.HasValue将从 virtual 改为 abstract.
Group.Synchronized(Group)和Match.Synchronized(Match) 在System.Text.RegularExpressions namespace中被移出.
System.Windows.Visibility enumeration 根本类型从int改换为byte.
ScriptObject 不再实现IDisposable (if you happened to be using that).
HtmlDocument.GetElementById 是从"GetElementByID"重命名来.
HtmlElement.Id 是从“ID”重命名来.
Scriptable endpoints必须使用HtmlPage.RegisterScriptableObject注册. 这个method 使用现有的System.Windows.WebApplication类型.
你不再需要在一个类的上标记[Scriptable] attribute来注册它. 而是你必须有在最后的PME上注册实例标记就会像[Scriptable]成功注册实例样.
ScriptObject.GetProperty<T>泛化类型被移出.取代的是, 你必须明确指出返回值类型.
ScriptObject.GetProperty and ScriptObject.Invoke/InvokeSelf返回值的类型为object.
返回值指向DOM objects或JavaScript objects(HtmlDocument, HtmlElement, HtmlObject, ScriptObject)是动态的返回大部分派生类型.可是,一个开发者仍然需要明确指定适当的类型type.
所有从GetProperty和Invoke/InvokeSelf值返回的值类型是numeric因为double有cross-browser的限制.如果你期待Int32值, 你现在必须明确使用Convert.ToInt32转换返回值的类型numeric为Int32类型.
HttpUtility type’s public signature是变为一对 HtmlEncode/Decode 和UrlEncode/Decode methods.
在System.Silverlight.dll中的System.Windows.ApplicationUnhandledExceptionEventArgs类型被移出,也随着在System.Windows.WebApplication中的 ApplicationUnhandledException的事件的使用.开发者应该开始使用unhandled exception支持新的 Windows Presentation Foundation (WPF)应用程序类型.
System.Windows.Browser.BrowserInformation.BrowserVersion的返回类型从 string改为System.Version.
System.ApplicationException 已经被废弃并且从涉及它的class中移出.
MultiScaleImage使用时有的a Viewport属性是一个Rect类型.它已经改换为两个属性, ViewportOrigin (是Point类型)和ViewportWidth (是Double).
Property Value Changes
下面属性中PointCollection替换Point[]:
System.Windows.Media.PolyLineSegment.Points
System.Windows.Media.PolyBezierSegment.Points
System.Windows.Media.PolyQuadraticBezierSegment.Points
System.Windows.Shapes.Polygon.Points
System.Windows.Shapes.Polyline.Points
下面属性是DoubleCollection代替Double[]:
System.Windows.Shapes.Shape.StrokeDashArray
Parameter名改换 大部分适应Visual Basic应用程序)
RenderTargetBitmap.Render 参数名字从'element'改为 'visual'
MediaElement.GetValue/SetValue: 参数名字从 'property' 改为 'dp'
UIElement.TransformToVisual: 参数名字从 'uiElement' 改为 'visual'
Dependency Properties加上 Private
TransformGroup.Value现在是一个 private DP
Geometry.Bounds 现在是一个 private DP
FrameworkElement.Resources 现在是一个 DP
TextBlock.Inlines 现在是一个 private DP
Run.Text 现在是一个 private DP
EventTrigger.RoutedEvent 现在是一个 private DP
KeySpline.ControlPoint1 和 ControlPoint2 现在是一个 DPs
Stroke.StylusPoints 现在是一个 a private DP
Stroke.DrawingAttributes 现在是一个 private DP
StylusPoint.X, Y, PressureFactor, Color, OutlineColor, Width, and Height 现在是一个 private DPs
StylusPoint.PressureFactor 现在不再产生accessors (DP only)
TextBox.SelectedText, SelectionLength,和SelectionStart 现在是一个 private DPs
RowDefinition.ActualHeight, ActualWidth 现在是一个 private DPs
(26)添加System.Net.Sockets Namespace
System.Net.Sockets namespace添加进Silverlight 2中为开发者需要紧密的控制网络提供一个sockets networking interface托管实现.看Working with Sockets.
------------works guo(翻译)