DZone
Mobile Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Mobile Zone > Page orientation not changing? There is a solution for that!

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

Denzel D. user avatar by
Denzel D.
·
Nov. 20, 10 · Mobile Zone · Interview
Like (0)
Save
Tweet
7.69K Views

Join the DZone community and get the full member experience.

Join For Free

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)
{
Debug.WriteLine("Changed");
}

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)
{
base.OnOrientationChanged(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.

Windows Phone application Event Workaround Debug (command) Testing React (JavaScript library) IT Machine

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Why Performance Projects Fail
  • Pattern Matching for Switch
  • Implementing RBAC Configuration for Kubernetes Applications
  • Ultra-Fast Microservices: When Microstream Meets Payara

Comments

Mobile Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo