xnawindows-phone-7

How to determine orientation of Windows Phone 7 in XNA game?


Similar to this question, but looking for an answer that will work in the context of an XNA game.

How can I determine whether the device is in a landscape or portrait orientation? The answer given in the general question relies upon functionality built into PhoneApplicationPage. AFAIK, you wouldn't normally be using that class within the context of an XNA game on Windows Phone 7.


Solution

  • From Nick Gravelyn: http://forums.xna.com/forums/p/49684/298915.aspx#298915 Accelerometer isn't in the XNA Framework anymore. You can access it through these steps:

    1. Add a reference to Microsoft.Devices.Sensors.dll
    2. Add 'using Microsoft.Devices.Sensors;' to your using statements.
    3. Hook up an event and start reading the accelerometer:

    Try this:

    try  
    {  
        AccelerometerSensor.Default.ReadingChanged += Default_ReadingChanged;  
        AccelerometerSensor.Default.Start();  
    }  
    catch (AccelerometerStartFailedException)  
    {  
    }
    
    1. Add the event handler itself:

    Like this:

    void Default_ReadingChanged(object sender, AccelerometerReadingAsyncEventArgs e)  
    {  
    }
    

    And you're good to go. Keep in mind, though, that accelerometer doesn't work with the emulator so there's no way to really test this without a device. You do need that try/catch because Start will throw an exception in the emulator because it doesn't support accelerometer.