将wp7应用移植到metro主要做这两方面的工作:
- 换ui控件,因为silverlight的ui控件不可以直接用在metro上
- 换api,silverlight的api同样也不能直接用在windows runtime上
在移植前,需要考虑以下因素
- 分辨率,wp7统一为480*800,但是win8支持各种分辨率
- 输入,wp7是触屏,而win8不仅触屏,还支持鼠标,键盘
- 硬件特性,例如手机通信网络,GSM,3G
- XNA, win8目前还不支持xna, 而是用directx来做3D
移植流程
- 使用vs11建立一个metro工程
- 将wp7工程的所有文件全都拷到新工程的目录下,选中“显示所有文件”并且包含在项目中
- 将所有的 命名空间 System.Windows 替换为 Windows.UI.Xaml
- 修改xaml文件,win8下只有grid可用,而PhoneApplicationPage则不可用了
- 编译,根据提示的错误,用windows runtime api 替换 .net api for metro styles
改进可移植性
- 最好别用一些设计模式,比如MVVM这种分享UI和逻辑的框架,因为有些代码可能是某些平台特有的
- 将一些可重用的代码分离出来,在项目中以 添加已有项 的方式来加代码,并且在 添加 时选择 以链接方式添加。(在添加的右下角按钮可以选择)
- 如果在共享文件中有平台特有代码,需要使用条件编译(xaml不支持)
- 选择 可移植类库 来创建跨平台库
移植UI,这里有一个UI库对应表
Silverlight XAML namespaces | Windows Runtime XAML namespaces |
---|---|
System.Windows | Windows.UI.Xaml |
System.Windows.Automation | Windows.UI.Xaml.Automation |
System.Windows.Automation.Peers | Windows.UI.Xaml.Automation.Peers |
System.Windows.Automation.Provider | Windows.UI.Xaml.Automation.Provider |
System.Windows.Automation.Text | Windows.UI.Xaml.Automation.Text |
System.Windows.Controls | Windows.UI.Xaml.Controls |
System.Windows.Controls.Primitives | Windows.UI.Xaml.Controls.Primitives |
System.Windows.Data | Windows.UI.Xaml.Data |
System.Windows.Documents | Windows.UI.Xaml.Documents |
System.Windows.Input | Windows.UI.Xaml.Input |
System.Windows.Interop | Windows.UI.Xaml.Interop |
System.Windows.Markup | Windows.UI.Xaml.Markup |
System.Windows.Media | Windows.UI.Xaml.Media |
System.Windows.Media.Animation | Windows.UI.Xaml.Media.Animation |
System.Windows.Media.Imaging | Windows.UI.Xaml.Media.Imaging |
System.Windows.Media.Media3D | Windows.UI.Xaml.Media.Media3D |
System.Windows.Navigation | Windows.UI.Xaml.Navigation |
System.Windows.Printing | Windows.UI.Xaml.Printing |
System.Windows.Resources | Windows.UI.Xaml.Resources |
System.Windows.Shapes | Windows.UI.Xaml.Shapes |
但是下面几个命名空间在windows runtime上没有对应的
- System.Windows.Ink
- System.Windows.Media.Effects
- System.Windows.Messaging
XAML在Silveright与metro上使用的不同
布局: windows runtime上没有 Pivot 和 Panorama,取而代之的是FlipView
windows runtime上没有 ApplicationBar,,取而代之的是AppBar
一些在屏幕方向上的不同
磁片和通知:大部分都一样,但是有一些细节不同
数据:metro同样没有内建客户端数据库, 更新ui同样是实现INotifyPropertyChanged和INotifyCollection
页面导航:同样是通过后退来导航,但是metro应用导航时不用提供具体的uri地址,只需要指明目标页面的在x:Class的属性里定义的类型即可,但是不支持清栈和uri mapping.
关于metro和silverlight的一些api的映射关系如下
API type | Silverlight API namespaces | Windows Runtime API namespaces (C#/Visual Basic) |
---|---|---|
Devices | Microsoft.Devices, Microsoft.Devices.Radio, Microsoft.Devices.Sensors | Windows.Devices.Enumeration, Windows.Devices.Enumeration.Pnp, Windows.Devices.Input, Windows.Devices.Sensors |
Windows Phone app model and environment | Microsoft.Phone, Microsoft.Phone.Info, Microsoft.Phone.Notification, Microsoft.Phone.Reactive, Microsoft.Phone.Shell, Microsoft.Phone.Tasks | Windows.Networking.PushNotifications, Windows.Devices.Sms, Windows.ApplicationModel.Background, Windows.ApplicationModel.Contacts, Windows.ApplicationModel.Contacts.Provider, Windows.ApplicationModel.Core |
Maps | Microsoft.Phone.Controls.Maps, Microsoft.Phone.Controls.Maps.AutomationPeers, Microsoft.Phone.Controls.Maps.Core Microsoft.Phone.Controls.Maps.Design Microsoft.Phone.Controls.Maps.Overlays, Microsoft.Phone.Controls.Maps.Platform | Windows.Devices.Geolocation |
Marketplace | Microsoft.Phone.Marketplace | Windows.ApplicationModel.Store |
Networking and syndication | Microsoft.Phone.Net, Microsoft.Phone.Net.NetworkInformation | Windows.Networking, Windows.Networking.BackgroundTransfer, Windows.Networking.Connectivity, Windows.Networking.NetworkOperators, Windows.Networking.Sockets, Windows.Web.AtomPub, Windows.Web.Syndication |
Programming and data models | System | Windows.Foundation, Windows.Foundation.Collections, Windows.Foundation.Metadata, Windows.Data.Xml.Dom, Windows.Data.Xml.Xsl, Windows.Data.Json |
Location | System.Device.Location | Windows.Devices.Geolocation, Windows.Networking.Proximity |
Automation and diagnostics | System.Diagnostics, System.Windows.Automation.Peers | Windows.Foundation.Diagnostics |
Graphics (3D transforms) | XNA Framework Class Library,Content Pipeline Class Library | No equivalent at this time. |
Controls and UI infrastructure | Microsoft.Phone.Controls, Microsoft.Phone.Controls.Primitives, | Windows.UI.ApplicationSettings, Windows.UI.Core, Windows.UI.Input, Windows.UI.Notifications, Windows.UI.ViewManagement |
Storage | System.IO.IsolatedStorage class | Windows.Storage, Windows.Storage.FileProperties |