Sunday, 27 October 2013

Windows 8.1 Api Changes : DisplayInformation.OrientationChanged


I wasted quite a bit of time this morning trying to figure out how to migrate from the old DisplayProperties.Orientation changed to this new api.

The crux of the confusion is the inconsistent implementation:  The old implementation was static, where the new event is on an instance.

That begs the question, where do I get the instance from?

Looking around the various samples, it appears that the best location is

DisplayInformation.GetForCurrentView(); which returns a DisplayInformation that I can wire up.

Now, the worry was, what happens when I move the CurrentView to another screen?  How do I get that new instance?

I made a couple of wrong guesses, including hooking up to the only static that looks like it might fire on DisplayInformation.DisplayContentsInvalidated.  In there, I checked to see if my new DisplayInformation was equal to one I had saved earlier from GetForCurrentView.

To my surprise, using object equality, they were always the same, even though the had different values!

So, GetForCurrentView*( appears to always return the same object, with different values.

As a consequence, it appears that the correct way to wire up is to hook up to the OrientationChanged event on the DisplayInformation from DisplayInformation.GetForCurrentView() which isn’t exactly obvious.

Hope this helps someone else.

It’s worse that it appeared.  DisplayContentsInvalidated appears to pass a null DisplayInformation when the device is actually rotated.

No comments:

Post a Comment