微软搞出的这个Universal应用本意是不错的,但是只是方便了WinRT应用到WPRT应用的移植,从WP8 Silverlight应用移植的话有很多坑,而且MSDN上面又没有一个完整详尽的说明,大部分新的实现方法都需要开发者自行摸索。所以我打算写这个系列文章来记录一些我的发现和经验,以帮助后来者少走一些弯路,节省一些时间。
RT应用的利与弊(随时补充)
首先我们来看一下移植到Runtime后的好处和坑处:
好处:
• 更少的内存消耗和更流畅的过渡动画。
• 支持更丰富的Tile类型。
• 提供强大的性能分析工具(Debug->Performance and Diagnostics)。
• 支持直接设置部分控件的RequestedTheme,不用操心Dark/Light主题的颜色适配问题。
• 应用设置可以漫游,方便WinRT/WPRT之间的设置同步。
坑:
• Pivot控件在左右切换时偶尔会闪烁。
• ListView控件在更新数据源时可能会出现短暂的黑块或空白块以及闪烁。
• 有时会有莫名其妙的不带Call Stack的异常抛出,不方便定位问题源。
• ListView控件在点按ListViewItem时整个控件可能会发生轻微的左右偏移现象。
• 目前不支持系统剪贴板。
• 应用被Suspend后导航栈会被清空,Resume时会重新进入MainPage(或其他默认首页),需要自己保存导航栈以及导航参数。
• 替代Panorama控件的Hub控件的HubSection目前只支持DataTemplate,不方便直接操作内部命名控件。
如何在Runtime下实现原Silverlight应用的部分操作(随时补充)
// Silverlight
this.Dispatcher.BeginInvoke(() => {});
// Runtime
this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => {});
this.Dispatcher.BeginInvoke(() => {});
// Runtime
this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => {});
// Silverlight
ProgressIndicator pi = new ProgressIndicator();
pi.Text = "text";
pi.IsIndeterminate = false; // or true
pi.IsVisible = true;
SystemTray.SetProgressIndicator(this, pi);
// Runtime
StatusBar.GetForCurrentView().ShowAsync();
StatusBar.GetForCurrentView().ProgressIndicator.ProgressValue = 0; // or null
StatusBar.GetForCurrentView().ProgressIndicator.Text = "text";
StatusBar.GetForCurrentView().ProgressIndicator.ShowAsync();
ProgressIndicator pi = new ProgressIndicator();
pi.Text = "text";
pi.IsIndeterminate = false; // or true
pi.IsVisible = true;
SystemTray.SetProgressIndicator(this, pi);
// Runtime
StatusBar.GetForCurrentView().ShowAsync();
StatusBar.GetForCurrentView().ProgressIndicator.ProgressValue = 0; // or null
StatusBar.GetForCurrentView().ProgressIndicator.Text = "text";
StatusBar.GetForCurrentView().ProgressIndicator.ShowAsync();
// Silverlight
MessageBox.Show("text", "title", MessageBoxButton.OK);
// Runtime
MessageDialog md = new MessageDialog("text", "title");
md.ShowAsync().GetResults();
MessageBox.Show("text", "title", MessageBoxButton.OK);
// Runtime
MessageDialog md = new MessageDialog("text", "title");
md.ShowAsync().GetResults();
// Silverlight
btn = new ApplicationBarIconButton(new Uri("/Assets/Icon/btn.png", UriKind.Relative));
btn.Text = "text";
this.ApplicationBar.Buttons.Add(btn);
menu = new ApplicationBarMenuItem("text");
this.ApplicationBar.MenuItems.Add(menu);
// Runtime
btn = new AppBarButton();
btn.Icon = new BitmapIcon();
(btn.Icon as BitmapIcon).UriSource = new Uri("ms-appx:///Assets/Icon/btn.png");
btn.Label = "text";
this.BottomAppBar.PrimaryCommands.Add(btn);
menu = new AppBarButton();
menu.Label = "text";
this.BottomAppBar.SecondaryCommands.Add(menu);
btn = new ApplicationBarIconButton(new Uri("/Assets/Icon/btn.png", UriKind.Relative));
btn.Text = "text";
this.ApplicationBar.Buttons.Add(btn);
menu = new ApplicationBarMenuItem("text");
this.ApplicationBar.MenuItems.Add(menu);
// Runtime
btn = new AppBarButton();
btn.Icon = new BitmapIcon();
(btn.Icon as BitmapIcon).UriSource = new Uri("ms-appx:///Assets/Icon/btn.png");
btn.Label = "text";
this.BottomAppBar.PrimaryCommands.Add(btn);
menu = new AppBarButton();
menu.Label = "text";
this.BottomAppBar.SecondaryCommands.Add(menu);
// Silverlight
SupportedOrientations="Portrait" // Page的XAML中设置
// Runtime
DisplayInformation.AutoRotationPreferences = DisplayOrientations.Portrait;
SupportedOrientations="Portrait" // Page的XAML中设置
// Runtime
DisplayInformation.AutoRotationPreferences = DisplayOrientations.Portrait;
// Silverlight
this.NavigationService.Navigate(new Uri("/Page.xaml?id=123", UriKind.Relative));
// Runtime
this.Frame.Navigate(typeof(Page), 123);
this.NavigationService.Navigate(new Uri("/Page.xaml?id=123", UriKind.Relative));
// Runtime
this.Frame.Navigate(typeof(Page), 123);
// Silverlight
MarketplaceReviewTask mrt = new MarketplaceReviewTask();
mrt.Show();
// Runtime
string appId = ""; // Store中的app GUID
await Launcher.LaunchUriAsync(new Uri("ms-windows-store:reviewapp?appid=" + Uri.EscapeDataString(appId)));
MarketplaceReviewTask mrt = new MarketplaceReviewTask();
mrt.Show();
// Runtime
string appId = ""; // Store中的app GUID
await Launcher.LaunchUriAsync(new Uri("ms-windows-store:reviewapp?appid=" + Uri.EscapeDataString(appId)));
» 转载请注明来源及链接:未来代码研究所
nice,感谢分享。 博主怎么解决HubSection访问内部控件问题的?
打算抛弃Hub,用FlipView实现类似Pivot的功能……如果实在要用Hub的话也不是没办法,可以用VisualTreeHelper强行访问控件或用数据绑定实现相关功能。
在最新的VS中创建wp项目默认就是用runtime,如何默认用Silverlight呢?
找名字里带Silverlight的project来创建
请问在runtime下 IsolatedStorageSettings对应哪个接口?
ApplicationData.Current.LocalSettings