我已将我的项目从 Windows 8.0 升级到 Windows 8.1,并收到一些关于过时代码的警告。其中一些我已修复,而另一些则没有。
这是我无法修复且找不到任何信息的最后警告的图像。
所有的warning都指向同一个方法,而且都说它已经过时了,我应该怎么做才能得到不是过时的代码?
代码如下:
警告编号 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(); }
警告编号 1。
// Set the initial visual state of the control VisualStateManager.GoToState(control, DetermineVisualState(ApplicationView.Value), false);
警告编号 3。
string visualState = DetermineVisualState(ApplicationView.Value);
以上所有代码,都调用了 DetermineVisualState 方法,该方法已弃用,它提供直接查询窗口布局大小,但这是什么意思?
注意:是LayoutAwarePage,所以我这里没有写任何代码,这是Windows 8.0实现。
如有任何帮助,我们将不胜感激,并提前致谢!
最佳答案
来自 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.
关于c# - Windows 8.1 如何修复这个过时的代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19586187/