如何在Windows Phone 8.1中设置支持的方向属性

11

我之前写过一个WP 8的应用程序,现在正在更新为WP 8.1。

自从最初发布以来,我的XAML和C#技能有了很大提高,因此我决定从头开始重写它,以避免挖掘旧的、新手级别的代码(是的...看起来不太好)。

有一件事情让我感到困惑,那就是如何处理应用程序的方向更改的启用和禁用。我已经找到了一种方法,在“Package.appmanifest”中实现完全的启用/禁用。然而,这不是我想要的。

在旧版本中,我只是在我的应用程序页面顶部写了这个:

<phone:PhoneApplicationPage
SupportedOrientations="PortraitOrLandscape"
etc...
etc...
>

这对我非常合适,因为有些页面在横向和纵向模式下都无法正常工作。(我花了比我想象中更多的时间来尝试使其正常工作……)但在8.1上无法使用。

有没有好心人知道如何在Windows Phone 8.1中为每个页面设置所需的方向支持?

1个回答

29

如果你想只显示竖屏,你可以这样做

DisplayInformation.AutoRotationPreferences = DisplayOrientations.Portrait;

如果您想要竖屏和横屏,请选择这个选项。

DisplayInformation.AutoRotationPreferences = DisplayOrientations.Portrait | DisplayOrientations.Landscape;

或者如果您只想要横向和翻转后的横向

DisplayInformation.AutoRotationPreferences = DisplayOrientations.LandscapeFlipped | DisplayOrientations.Landscape;

每个页面等等,因此您可以根据页面和使用意图启用/禁用方向。例如,您可以在OnNavigatedTo事件处理程序中设置它。

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    DisplayInformation.AutoRotationPreferences = DisplayOrientations.LandscapeFlipped | DisplayOrientations.Landscape;

    this.navigationHelper.OnNavigatedTo(e);
}

在这里阅读有关于DisplayInformation.AutoRotationPreferences的更多信息。


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