Windows Phone 7 contains a hardware-based sensor called the accelerometer. The accelerometer measures the phone's position relative to a line drawn from the phone to the center of the Earth, and is measured on three axes (x, y, z) in relation to this line.
The x-axis is the horizontal axis of the phone when the phone is held in the portrait orientation. The y-axis is the vertical axis when the phone is held in the portrait orientation. The z-axis is a line perpendicular to the surface of the phone. Figure 20-1 shows a representation of these three axes, as well as possible values for each axis.
The Accelerometer class is part of the
Microsoft.Devices.Sensors namespace. To use the accelerometer you first need to add references in your project to the
Microsoft.Phone.Reactive assemblies. Then you will need to add a using reference to the following three namespaces:
using Microsoft.Devices; using Microsoft.Devices.Sensors; using Microsoft.Phone.Reactive;
Once you have these references in place you can create a new
private readonly Accelerometer accelerometer = new Accelerometer();
Before using the accelerometer you should check the state of the accelerometer sensor. You can do this using the
Accelerometer.State property, which returns the current state of the accelerometer as an enumeration of type ...