Using the Accelerometer Class

,

Accelerometer, like the other sensor types, uses an event-based model for monitoring changes to sensor readings.


Note

Accelerometer contains a ReadingChanged event, which should not be used because it is a remnant of the first release of the Windows Phone OS and has been deprecated as of the 7.1 release of the SDK.


The event to use is the base class SensorBase event: CurrentValueChanged. Subscribing to the event can be done as follows:

var accelerometer = new Accelerometer();
accelerometer.CurrentValueChanged += HandleSensorValueChanged;
accelerometer.Start();

When the CurrentValueChanged event is raised, the event handler receives a SensorReadingEventArgs<AccelerometerReading> object, as shown:

void HandleSensorValueChanged(
    object sender, SensorReadingEventArgs<AccelerometerReading> e)
{
    ProcessReading(e.SensorReading);
}

SensorReadingEventArgs provides a reference to the Accelerometer via its single Sensor property. AccelerometerReading contains an Acceleration property, which is a Microsoft.Xna.Framework.Vector3. XAML apps do not have a type representing a three-dimensional vector baked into the SDK; thus, there is some crossover here, and Vector3 is used throughout the phone sensor API.

Rather than relying on the XNA Vector3 type in the sample code, a custom ThreeDimensionalVector class is used that has a markedly simpler implementation and uses properties rather than fields to access vector dimension values, which improves its compatibility with the XAMLdata binding infrastructure.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.81.57