Similar to this question, but looking f开发者_JAVA百科or 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.
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:
- Add a reference to Microsoft.Devices.Sensors.dll
- Add 'using Microsoft.Devices.Sensors;' to your using statements.
- Hook up an event and start reading the accelerometer:
Try this:
try
{
AccelerometerSensor.Default.ReadingChanged += Default_ReadingChanged;
AccelerometerSensor.Default.Start();
}
catch (AccelerometerStartFailedException)
{
}
- 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.
This has changed it seems. In order to hook into the 'reading changed' you have to create an accelerometer, and then start it. The code required can be found at MSDN. Retrieving Accelerometer Input (Windows Phone)
it looks something like this:
#if WINDOWS_PHONE
Accelerometer accelerometer;
try
{
accelerometer = new Accelerometer();
accelerometer.ReadingChanged += new EventHandler<AccelerometerReadingEventArgs>(a_ReadingChanged);
accelerometer.Start();
}
catch (AccelerometerFailedException e)
{
}
...
}
void a_ReadingChanged(object sender, AccelerometerReadingEventArgs e)
{
//this function is not supported by the window 7 phone emulator
throw new NotImplementedException();
}
#endif
Here's a post from Shawn Hargreaves' Blog
http://blogs.msdn.com/b/shawnhar/archive/2010/07/12/orientation-and-rotation-on-windows-phone.aspx?utm_source=twitterfeed&utm_medium=twitter
If you want to automatically switch between both landscape and portrait orientations as the phone is rotated:
graphics.SupportedOrientations = DisplayOrientation.Portrait |
DisplayOrientation.LandscapeLeft |
DisplayOrientation.LandscapeRight;
Switching between LandscapeLeft and LandscapeRight can be handled automatically with no special help from the game, and is therefore enabled by default. But switching between landscape and portrait alters the backbuffer dimensions (short-and-wide vs. tall-and-thin), which will most likely require you to adjust your screen layout. Not all games will be able to handle this (and some designs only make sense one way up), so dynamic switching between landscape and portrait is only enabled for games that explicitly opt-in by setting SupportedOrientations.
精彩评论