Over a million developers have joined DZone.

Page orientation not changing? There is a solution for that!

· Mobile Zone

Today I was introducing a fellow developre to the fun world of Windows Phone 7 development. In the process of explaining the basic principles, we reached the topic of changing page orientations. And when it came to the point where I had to provide a working sample of a page that reacts to a change orientations, I noticed that my sample failed.

In some cases when you work on a Windows Phone 7 application on your developer machine and you debug your application in the emulator, you might experience an unexpected consequence to a change in page orientation. To be specific - you will see no effect at all when you use the rotation buttons in the emulator. The OrientationChanged event handler is never triggered.

The first thing I did is check the properties set for the page that define the page orientation:

SupportedOrientations="PortraitOrLandscape" Orientation="Portrait"

No problems here. I decided to set a breakpoint on the event handler, so that I can track exactly when the orientation changes. But it never reached that.

So I decided to take it further and experiment with device rotation in XNA.

This is what I had in the Game() constructor:

graphics.SupportedOrientations = DisplayOrientation.Default | DisplayOrientation.LandscapeLeft
| DisplayOrientation.LandscapeRight | DisplayOrientation.Portrait;

Window.OrientationChanged += new EventHandler<EventArgs>(Window_OrientationChanged);

The event handler was so simple that I don't even have to explain it's purpose:

void Window_OrientationChanged(object sender, EventArgs e)

The results were a surprise to me:

The orientation was changing in an XNA game and not in a Silverlight-based application. It did seem to me that this is a framework-related bug. However, I noticed that sometimes it appeared and sometimes it didn't. What I've noticed is that there are two workarounds available for this problem.

The first one, you need to override the default OnOrientationChanged for the page:

protected override void OnOrientationChanged(OrientationChangedEventArgs e)

And it worked - the OrientationChanged event handler started working. Another solution would be forcing the page to change the orientation manually, but this is only a solution when you want to manually trigger the change and isn't really reliable (you might need to tie it to the Accelerometer - and you won't be able to test it in the emulator).

What needs to be done is directly enforcing the SupportedOrientation limit:

this.SupportedOrientations = SupportedPageOrientation.Landscape;
this.Orientation = PageOrientation.LandscapeLeft;

If you've encountered this issue and have another workaround, let me know and I will update this post.


Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}