Windows 8.1如何修复这个过时的代码?

Mis*_*sky 9 c# windows obsolete windows-store-apps

我已经从Windows 8.0将我的项目升级到Windows 8.1,并获得了一些过时代码的警告.其中一些我已修复,其中一些没有.

这是我无法修复的最后警告的图像,无法找到任何信息.

在此输入图像描述

所有警告都指的是相同的方法,它说它已经过时了,我该怎么办才能获得不过时的代码?

以下是代码:

  1. 警告号码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();
    }
    
    Run Code Online (Sandbox Code Playgroud)
  2. 警告编号1.

    // Set the initial visual state of the control
    VisualStateManager.GoToState(control, DetermineVisualState(ApplicationView.Value), false);
    
    Run Code Online (Sandbox Code Playgroud)
  3. 警告3号.

    string visualState = DetermineVisualState(ApplicationView.Value);
    
    Run Code Online (Sandbox Code Playgroud)

以上所有代码都调用了不推荐使用的DetermineVisualState方法,它提供了直接查询窗口布局大小的含义,但这是什么意思?

注意:它是LayoutAwarePage,所以我这里没有写任何代码,这是Windows 8.0的实现.

任何帮助将不胜感激,并提前感谢!

Xyr*_*oid 8

从MSDN:如何停止使用LayoutAwarePage

在Windows 8中,Microsoft Visual Studio模板生成LayoutAwarePage类以基于ApplicationViewState管理可视状态.在Windows 8.1中,不推荐使用ApplicationViewState,并且不再在Windows应用商店应用的Visual Studio模板中包含LayoutAwarePage.继续使用LayoutAwarePage可能会破坏您的应用程序.要解决此问题,请重写视图以适应新的最小视图状态,并根据窗口大小创建事件.如果您将应用更新为不同的大小,则必须处理Window.Current和Window.SizeChanged事件,以使应用的UI适应新的大小.我们建议您停止使用LayoutAwarePage,并直接从Page类继承类.以下是如何停止使用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);
     }
 }
Run Code Online (Sandbox Code Playgroud)

Retargeting to Windows 8.1 Preview在此链接中搜索

打开LayoutAwarePage并将DetermineVisualState方法更改为不再依赖ApplicationViewState,而是依赖于窗口大小.例如,您可以在应用程序窗口宽度小于500px时返回VerticalLayout,在大于500px时返回Horizo​​ntalLayout.由于此方法是虚拟的,因此它被设计为在需要时在每个页面中被覆盖(就像在SplitPage上完成的那样).如果布局和视觉状态不同,您可以在任何页面上覆盖它.只需确保重命名每个页面上的可视状态以匹配这些新字符串.

  • 没问题,谢谢你建议我加入总结:) (2认同)
  • MSDN文章的问题在于"我们建议您停止使用LayoutAwarePage类,并直接从Page类继承类." 它忽略了在LayoutAwarePage中有大量其他模板代码用于导航,导航状态和默认视图模型. (2认同)