A group blog from members of the VB team
Previously, I explained how to create a panorama and pivot effect for Windows Phone 7. In this blog post, I want to share a sample that will help you to create an accelerometer for Windows Phone 7. An accelerometer is a device that measures the proper acceleration of the device. It is a sensor that measures acceleration forces caused by moving the sensor. In this application the accelerometer feature will allow you to determine the orientation and the motion of the phone.
Now I will demonstrate how to create an accelerometer for Windows Phone 7, using Visual Basic for Windows Phone Developer Tools. Sounds interesting? So let’s begin.
But hey, wait a minute! Before you create the accelerometer application, you need to install the following applications:
The accelerometer can be created in 4 simple steps as follows:
<TextBlock VerticalAlignment="Top" Text="x:" Name="XLabel" Style="{StaticResource PhoneTextNormalStyle}" Margin="42,124,386,0"></TextBlock>
<TextBlock VerticalAlignment="Top" Text=" " Name="XTextBlock" Style="{StaticResource PhoneTextExtraLargeStyle}" Margin="66,100,20,0" Foreground="{StaticResource PhoneAccentBrush}"></TextBlock>
<TextBlock VerticalAlignment="Top" Text="y:" Name="YLabel" Style="{StaticResource PhoneTextNormalStyle}" Margin="42,236,386,0"></TextBlock>
<TextBlock VerticalAlignment="Top" Text=" " Name="YTextBlock" Style="{StaticResource PhoneTextExtraLargeStyle}" Margin="68,212,52,0" Foreground="{StaticResource PhoneAccentBrush}"></TextBlock>
<TextBlock VerticalAlignment="Top" Text="z:" Name="ZLabel" Style="{StaticResource PhoneTextNormalStyle}" Margin="42,332,386,0"></TextBlock>
<TextBlock VerticalAlignment="Top" Text=" " Name="ZTextBlock" Style="{StaticResource PhoneTextExtraLargeStyle}" Margin="66,308,54,0" Foreground="{StaticResource PhoneAccentBrush}"></TextBlock>
<TextBlock VerticalAlignment="Top" Text="status:" Name="statusLabel" Style="{StaticResource PhoneTextNormalStyle}" Margin="24,11,0,0" HorizontalAlignment="Left" Width="72" />
<TextBlock VerticalAlignment="Top" Text="accelerometer stopped" Name="statusTextBlock" Style="{StaticResource PhoneTextNormalStyle}" Foreground="{StaticResource PhoneAccentBrush}" Margin="102,11,6,0" />
Your application now looks like this:
It is essential to add event handlers to the application because they help to initialize the application bar and control the accelerometer by handling the change in readings.
To add event handlers, do the following:
Inherits PhoneApplicationPage
Private accelerometer As Accelerometer
#Region "Initialization"
''' <summary>
''' Constructor for the PhoneApplicationPage object.
''' In this method, the Application Bar is initialized.
''' </summary>
Public Sub New()
InitializeComponent()
ApplicationBar = New ApplicationBar()
ApplicationBar.IsVisible = True
Dim startStopButton As New ApplicationBarIconButton(New Uri("/Images/startstop.png", UriKind.Relative))
startStopButton.Text = "on/off"
AddHandler startStopButton.Click, AddressOf startStopButton_Click
ApplicationBar.Buttons.Add(startStopButton)
End Sub
#End Region
#Region "User Interface"
''' Click handler for the start/stop button.
''' <param name="sender"></param>
''' <param name="e"></param>
Private Sub startStopButton_Click(ByVal sender As Object, ByVal e As EventArgs)
' If the accelerometer is null, it is initialized and started
If accelerometer Is Nothing Then
' Instantiate the accelerometer sensor object
accelerometer = New Accelerometer()
' Add an event handler for the ReadingChanged event.
AddHandler accelerometer.ReadingChanged, AddressOf accelerometer_ReadingChanged
' The Start method could throw and exception, so use a try block
Try
statusTextBlock.Text = "starting accelerometer"
accelerometer.Start()
Catch exception As AccelerometerFailedException
statusTextBlock.Text = "error starting accelerometer"
End Try
Else
' if the accelerometer is not null, call Stop
accelerometer.Stop()
accelerometer = Nothing
statusTextBlock.Text = "accelerometer stopped"
statusTextBlock.Text = "error stopping accelerometer"
End If
#Region "Accelerometer Event Handling"
''' The event handler for the accelerometer ReadingChanged event.
''' BeginInvoke is used to pass this event args object to the UI thread.
Private Sub accelerometer_ReadingChanged(ByVal sender As Object, ByVal e As AccelerometerReadingEventArgs)
Deployment.Current.Dispatcher.BeginInvoke(Sub() MyReadingChanged(e))
''' Method for handling the ReadingChanged event on the UI thread.
''' This sample just displays the reading value.
Private Sub MyReadingChanged(ByVal e As AccelerometerReadingEventArgs)
If accelerometer IsNot Nothing Then
statusTextBlock.Text = accelerometer.State.ToString()
XTextBlock.Text = e.X.ToString("0.00")
YTextBlock.Text = e.Y.ToString("0.00")
ZTextBlock.Text = e.Z.ToString("0.00")
There you are! Now your accelerometer application for Windows Phone 7 is ready! You just need to build and debug the application.
Note: To stop debugging the application, select Debug > Stop Debugging.
Finally, to submit your application to the market place, you can refer to upload your application walkthrough.
That’ it! Wasn’t that amazing? You have successfully created an accelerometer for Windows Phone 7, that too in just 4 steps! It’s pretty easy, right? I am sure you must have enjoyed and had a lot of fun creating this application.
You can find the full source code for the accelerometer application here. This application uses general Silverlight and Visual Basic features that are applicable for different application types including Windows Phone application.
Please, correct Item 5.
Select Machine.Device.Sensors from the list, and then click OK.
Change, Machine.Device by Microsoft.Device
I couldn't understand step 2 in 5th point properly about "To create an event handler that handles the change in readings".