Windows 8.1 How to fix this obsolete code?

2020-08-26 10:42发布

问题:

I have upgraded my project to windows 8.1 from windows 8.0 and got some warnings of obsolete codes. Some of them I have fixed, and some of them not.

Here is an image of the last warnings that I couldn't fix and couldn't find any information.

All warnings refers to the same method, and it says that it is obsolete, what should I do to get the not obsolete code?

Here are the codes:

  1. warning number 2.

    /// <summary>
    /// Translates <see cref="ApplicationViewState" /> values into strings for visual state
    /// management within the page.  The default implementation uses the names of enum values.
    /// Subclasses may override this method to control the mapping scheme used.
    /// </summary>
    /// <param name="viewState">View state for which a visual state is desired.</param>
    /// <returns>Visual state name used to drive the
    /// <see cref="VisualStateManager" /></returns>
    /// <seealso cref="InvalidateVisualState" />
    protected virtual string DetermineVisualState(ApplicationViewState viewState)
    {
        return viewState.ToString();
    }
    
  2. Warning number 1.

    // Set the initial visual state of the control
    VisualStateManager.GoToState(control, DetermineVisualState(ApplicationView.Value), false);
    
  3. Warning number 3.

    string visualState = DetermineVisualState(ApplicationView.Value);
    

All the above codes, calls to the DetermineVisualState method which is deprecated, it offers to query for window layout sizes directly, but what does it mean?

Note: It is the LayoutAwarePage, so I haven't wrote here any code, this is Windows 8.0 implementation.

Any help would be appreciated and thanks in advance !

回答1:

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.