How to set Supported orientations property in Wind

2019-01-25 08:16发布

问题:

I wrote an application for WP 8 some time ago, I'm currently working on updating it for WP 8.1.

My XAML and C#-skills have improved a lot since the initial launch, so I decided to rewrite it from scratch in order to avoid digging through old, noob code (yeah... it isn't pretty).

One thing that I can't seem to get my head around is how to deal with enabling and disabling orientation-changes for the app. I've found a way to make a total enable/disable with the "Package.appmanifest". That's not quite what I'm after however.

I simply wrote this at the top of my app pages in the old version:

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

This suited me very well since some pages simply didn't work in both portrait and landscape mode. (I spent more time than I care to remember trying to make it work...) But it won't work in 8.1.

Would some kind soul know of a way to set desired orientation-support per page in Windows Phone 8.1?

回答1:

You could do this if you wanted just portrait

DisplayInformation.AutoRotationPreferences = DisplayOrientations.Portrait;

Or this if you wanted Portrait and Landscape

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

Or if you wanted just Landscape and Landscape Flipped

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

etc. on every page, so you can enable/disable orientations depending on the page and how you intend to use it. You can set it in OnNavigatedTo event handler for example.

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

    this.navigationHelper.OnNavigatedTo(e);
}

Read more about DisplayInformation.AutoRotationPreferences here.