3. Coding the Application
In Solution Explorer, open MainPage.xaml.cs and replace the code there with the following C# code blocks that will implement the UI updates using accelerometer data.
3.1. Specifying the Namespaces
Begin by listing the namespaces the application will use. Notice our inclusion of Microsoft.Devices.Sensors that will allow you to start and stop Windows Phone's accelerometer.
using System;
using System.Windows;
using Microsoft.Phone.Controls;
using Microsoft.Devices.Sensors;
namespace CaptureAccelerometerData
{
public partial class MainPage : PhoneApplicationPage
{
3.2. Initializing Variables
The variable _ac, an Accelerometer object, will be used to start and stop, and retrieve x, y, z and time. Also notice the inclusion of the ReadingChanged event, which you'll draw on to send captured accelerometer data to your UI.
Accelerometer _ac;
public MainPage()
{
InitializeComponent();
_ac = new Accelerometer();
_ac.ReadingChanged += new
EventHandler<AccelerometerReadingEventArgs>(_ac_ReadingChanged);
}
3.3. Capturing and Displaying Accelerometer Data
Notice here that you cannot
directly change the UI elements upon receiving the accelerometer data
because the accelerometer data comes from a different thread than the
current UI thread. If you try to change the UI elements directly here
you will get an "Invalid cross-thread access" error, as shown in Figure 6-13. In order to overcome this problem, you must use the Dispatcher in the current UI thread, as shown in the following code.
private void ProcessAccelerometerReading(AccelerometerReadingEventArgs e)
{
txtTime.Text = e.Timestamp.ToString();
txtX.Text = e.X.ToString();
txtY.Text = e.Y.ToString();
txtZ.Text = e.Z.ToString();
txtPitch.Text = RadianToDegree((Math.Atan(e.X / Math.Sqrt(Math.Pow(e.Y, 2) +
Math.Pow(e.Z, 2))))).ToString();
txtRoll.Text = RadianToDegree((Math.Atan(e.Y / Math.Sqrt(Math.Pow(e.X, 2) +
Math.Pow(e.Z, 2))))).ToString();
txtTheta.Text = RadianToDegree((Math.Atan(Math.Sqrt(Math.Pow(e.X, 2) +
Math.Pow(e.Y, 2))/ e.Z))).ToString();
}
3.4. Implementing Start and Stop of Accelerometer
Implement the button event
for stopping and starting the accelerometer. Notice here that you must
anticipate the possible error that might occur when you are trying to
start or stop the accelerometer.
private void btnStart_Click(object sender, RoutedEventArgs e)
{
try
{
_ac.Start();
}
catch (AccelerometerFailedException)
{
MessageBox.Show("Acceleromter failed to start.");
}
}
private void btnStop_Click(object sender, RoutedEventArgs e)
{
try
{
_ac.Stop();
}
catch (AccelerometerFailedException)
{
MessageBox.Show("Acceleromter failed to stop.");
}
}
}
}
4. Testing the Finished Application
To test the finished application, press F5. The result should resemble the screenshot shown in Figure 6-10,
and you will see that the x, y, z, and time text blocks are constantly
being updated each time you click the Start button. Remember that to run
the application on a Windows Phone 7 device, you must choose the
"Windows Phone 7 Device" option shown in Figure 5.