Windows 8.1 How to fix this obsolete code?

半世苍凉 提交于 2019-12-05 02:44:14

From MSDN: How to stop using the LayoutAwarePage

In Windows 8, Microsoft Visual Studio templates generate the LayoutAwarePage class to manage the visual states based on the ApplicationViewState. In Windows 8.1, ApplicationViewState is deprecated and LayoutAwarePage is no longer included in the Visual Studio templates for Windows Store apps. Continuing to use the LayoutAwarePage can break your app. To fix this, rewrite your view to accommodate the new minimum view state, and create events based on the window size. If you update your app to different sizes, you must handle the Window.Current and Window.SizeChanged events to adapt the UI of your app to the new size. We recommend that you stop using the LayoutAwarePage, and inherit the classes directly from the Page class. Here's how to stop using the LayoutAwarePage:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    base.OnNavigatedTo(e);
    this.Loaded += PageLoaded;
    this.Unloaded += PageUnloaded;
 }

 private void PageUnloaded(object sender, RoutedEventArgs e)
 {
     Window.Current.SizeChanged -= Window_SizeChanged;
 }

 private void PageLoaded(object sender, RoutedEventArgs e)
 {
     Window.Current.SizeChanged += Window_SizeChanged;
 }

 private void Window_SizeChanged(object sender, Windows.UI.Core.WindowSizeChangedEventArgs e)
 {
     if (e.Size.Width <= 500)
     {
     //VisualStateManager.GoToState(this, state.State, transitions);
     }
     else if (e.Size.Height > e.Size.Width)
     {
     //VisualStateManager.GoToState(this, state.State, transitions);
     }
     else
     {
     //VisualStateManager.GoToState(this, state.State, transitions);
     }
 }

Search for Retargeting to Windows 8.1 Preview in this link

Open LayoutAwarePage and change the DetermineVisualState method to no longer rely on the ApplicationViewState and instead be dependent on the window size. For instance, you could return VerticalLayout when your app window width is less than 500px and HorizontalLayout when it’s greater than 500px. As this method is virtual, it is designed to be overridden in each page when needed (as it’s done on the SplitPage). You can override this on any page if your layouts and visual states differ. Just make sure to rename the visual states on each of your pages to match these new strings.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!