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

9
我已将我的项目从Windows 8.0升级到Windows 8.1,但收到了一些过时代码的警告。我已经修复了其中一些,但有些没有。
这是最后几个警告的图片,我无法修复并且找不到任何信息。
所有警告都指向同一个方法,并且它说它已经过时了,我该怎么做才能得到未过时的代码?
以下是代码:
  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);
    
以上所有代码都调用了被弃用的DetermineVisualState方法,它提供直接查询窗口布局大小的功能,但这意味着什么?
注意:这是LayoutAwarePage,所以我在这里没有编写任何代码,这是Windows 8.0实现。
感谢您的帮助和支持!

你看过这个Windows 8.1示例了吗?需要帮助吗? - Roger Rowland
是的,这是我看到的第一个示例,但它没有使用LayoutAwarePage,所以没有帮助。 - Misha Zaslavsky
1
没错 - 就像它所说的 "在Windows 8.1预览版中,LayoutAwarePage已从默认的Windows Store应用程序模板中删除" - 因此该文章提供了一个可能的替代方案。这对您不可行吗? - Roger Rowland
也许它会有所帮助,但我在所有页面中使用LayoutAwarePage,这是否会产生相同的效果?我能否更改少量代码以获得相同的结果? - Misha Zaslavsky
1个回答

8

来自 MSDN 的消息:如何停止使用 LayoutAwarePage

在 Windows 8 中,Microsoft Visual Studio 模板生成 LayoutAwarePage 类以根据 ApplicationViewState 管理视觉状态。在 Windows 8.1 中,ApplicationViewState 已被弃用,并且 Windows Store 应用程序的 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);
     }
 }

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

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


问题在于,如果链接失效了(这确实会发生),那么答案就变得无用了。最好在此处包含链接摘要,这样就不需要去另一个网站了。顺便说一句,踩票不是我给的,很可能来自于此元帖讨论 - Taryn
2
没问题,谢谢建议我包含摘要 :) - Farhan Ghumra
2
那篇MSDN文章的问题在于它说“我们建议您停止使用LayoutAwarePage类,并直接从Page类继承类。”,但它忽略了LayoutAwarePage中有大量其他模板代码用于导航、导航状态和默认视图模型。 - Luke Puplett

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接