9

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.

enter image description here

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 !

Misha Zaslavsky
  • 8,414
  • 11
  • 70
  • 116
  • Have you seen [this Windows 8.1 example](http://code.msdn.microsoft.com/windowsapps/Controlling-the-VisualState-d169aeb8)? Any help? – Roger Rowland Oct 25 '13 at 09:39
  • Yes, this is the first example I saw, no help because it's not uses LayoutAwarePage – Misha Zaslavsky Oct 25 '13 at 09:41
  • 1
    That's right - as it says *"With Windows 8.1 preview the LayoutAwarePage is removed from the default Windows Store app templates"* - so the article gives a possible alternative. Is that not possible for you? – Roger Rowland Oct 25 '13 at 09:57
  • Maybe, it may help, but I use the LayoutAwarePage in all my pages, is it making the same? and can I change not much code to get the same results? – Misha Zaslavsky Oct 25 '13 at 10:02

1 Answers1

8

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.

Farhan Ghumra
  • 15,180
  • 6
  • 50
  • 115
  • The problem is if the link ever dies (which does happen) then the answer is useless. It is preferable to include a summary of the link here so it is not necessary to go to another site. BTW the downvote is not mine, it most likely came from the discussion on [this meta post](http://meta.stackexchange.com/questions/202974/when-is-it-wrong-to-flag-a-link-only-answer) – Taryn Oct 25 '13 at 13:26
  • 2
    No problem though, thanks for suggesting me to include summary :) – Farhan Ghumra Oct 25 '13 at 13:34
  • 2
    The problem with that MSDN article is that in saying "We recommend that you stop using the LayoutAwarePage class, and inherit the classes directly from the Page class." it neglects to consider that there's a ton of other template code in LayoutAwarePage for navigation, nav state and the default view model. – Luke Puplett Nov 18 '14 at 17:06