views:

238

answers:

3

How can you tell whether the device is oriented vertically (portrait) or horizontally (landscape)?

Is there an API that simplifies this or do you have to make the determination "by hand" using the accelerometer?

+3  A: 

I myself just have looked at windows 7 phones(through vs2010 express phone edition).

It seems to have in the code behind this

 public MainPage()
        {
            InitializeComponent();
            // seems to set the supported orientations that your program will support.
            SupportedOrientations = SupportedPageOrientation.Portrait | SupportedPageOrientation.Landscape;
        }

Then the actual form has

  private void PhoneApplicationPage_OrientationChanging(object sender, OrientationChangedEventArgs e)
        {
            var test = e.Orientation;

        }

So when the orientation changes it e.Orientation will tell you what orientation it is. Like for instance LandscapeRight.

chobo2
A: 

You can also ask it through this.Orientation when your application starts so you know what the orientation is. Afther the start you can use the OrientationChanged event.

In your main:

OrientationChanged += new EventHandler<OrientationChangedEventArgs>(MainPage_OrientationChanged);

void MainPage_OrientationChanged(object sender, OrientationChangedEventArgs e)

{

   Console.WriteLine(e.Orientation.ToString());

}
matthijs Hoekstra
+1  A: 

Also you don't have to track this only via the event, you can ask for it directly from the PhoneApplicationPage instance:

private void Button_Click(object sender, RoutedEventArgs e)
{
    MyCurrentOrientation.Text = this.Orientation.ToString();
}
Shawn Oster