Forcing Orientation in WinRT


7d703de7-03a1-4a9b-8425-841ae065c30fI am working with a client on an enterprise Win8 app that is for order taking. They have a specific page that they require to be only in Portrait mode while the rest of the app can support any orientation. Since I've done so much Windows Phone 7/8 work I thought this would be simple. Just specify the value on the Page. But this didn't work…

Digging through the docs I found a probable solution: DisplayProperties.AutoRotationPreferences (in the Windows.Graphics.Display namespace). The docs specify that this property can be set with the DisplayOrientations enumeration to specify which of the four orientations to support. The enumeration is a flag so you can combine them too:

// All orientations
DisplayProperties.AutoRotationPreferences = DisplayOrientations.None;

// Portrait only
DisplayProperties.AutoRotationPreferences = DisplayOrientations.Portrait;

// Landscape only
DisplayProperties.AutoRotationPreferences = DisplayOrientations.Landscape;

// Landscape and upside down landscape only
DisplayProperties.AutoRotationPreferences = DisplayOrientations.Landscape | 
                                            DisplayOrientations.LandscapeFlipped;

Using these options should work, right? I tested it in a bunch of different places in my code and it didn't seem to have any effect. I was baffled.

Later I found an MSDN forum posting where they mentioned these work but only if the device has an accelerometer. This means on my laptop and even in the simulator these have *no* effect! When I did remote debugging to my Surface…it worked exactly the way I wanted it to work. Whew…the client never knew that I had to look it up.  Unless he reads my blog that is ;)



Shawn
Shawn Wildermuth
Author, Teacher, and Coach




My Courses

Wilder Minds Training
Vue.js by Example (New Lower Price)
Bootstrap 4 by Example (New Lower Price)
Intro to Font Awesome 5 (Free Course)
Pluralsight
Building an API with ASP.NET Core (New Course)
Building a Web App with ASP.NET Core, MVC6, EF Core, Bootstrap and Angular (updated for 2.2)
Less: Getting Started (New)
Using Visual Studio Code for ASP.NET Core Projects
Implementing ASP.NET Web API

Application Name WilderBlog Environment Name Production
Application Ver v4.0.30319 Runtime Framework x86
App Path D:\home\site\wwwroot\ Runtime Version .NET Core 4.6.27514.02
Operating System Microsoft Windows 10.0.14393 Runtime Arch X86