Window phone accelerator (gravity sensing) Initial Experience

Source: Internet
Author: User

I moved it from my Netease blog ....

 

The acceleration sensor of WP7. That is, the module that implements gravity sensing. Windows Phone

The acceleration sensor in the device is used to measure the acceleration of three coordinate axes. The approximate coordinates are as follows:

 

All we need to do is read the corresponding data from the Accelerometer object.

 

 

// Mainpage. the XAML code mainly includes the following code <grid. columndefinitions> <columndefinition width = "100"/> <columndefinition width = "300"/> </grid. columndefinitions> <grid. rowdefinitions> <rowdefinition Height = "100"/> <rowdefinition Height = "100"/> <rowdefinition Height = "100"/> <rowdefinition Height = "100"/> <rowdefinition height = "100" type = "parmname" text = "parmname"/> <rowdefinition Height = "100" type = "parmname" text = "/parmname"/> </grid. rowdefinitions> <textblock text = "X:" grid. column = "0" grid. row = "0" width = "88" Height = "82" horizontalalignment = "Left" verticalignment = "TOP" fontsize = "50"/> <textbox X: name = "tbx" width = "300" Height = "80" grid. column = "1" horizontalalignment = "Left" verticalalignment = "TOP" grid. row = "0"/> <textblock text = "Y:" grid. row = "1" grid. column = "0" horizontalalignment = "Left" verticalalignment = "TOP" width = "90" Height = "80" fontsize = "50"/> <textbox X: name = "tby" grid. row = "1" grid. column = "1" horizontalalignment = "Left" verticalalignment = "TOP" width = "300" Height = "80"/> <textblock text = "Z:" grid. column = "0" grid. row = "2" horizontalalignment = "Left" verticalalignment = "TOP" width = "90" Height = "80" fontsize = "50"/> <textbox X: name = "TBZ" grid. column = "1" grid. row = "2" width = "300" Height = "80" horizontalalignment = "Left" verticalignment = "TOP"/> <textblock text = "CX:" grid. column = "0" grid. row = "3" width = "88" Height = "82" horizontalalignment = "Left" verticalignment = "TOP" fontsize = "50"/> <textbox X: name = "tbcx" width = "300" Height = "80" grid. column = "1" horizontalalignment = "Left" verticalalignment = "TOP" grid. row = "3"/> <textblock text = "Cy:" grid. row = "4" grid. column = "0" horizontalalignment = "Left" verticalalignment = "TOP" width = "90" Height = "80" fontsize = "50"/> <textbox X: name = "tbcy" grid. row = "4" grid. column = "1" horizontalalignment = "Left" verticalalignment = "TOP" width = "300" Height = "80"/> <textblock text = "CZ:" grid. column = "0" grid. row = "5" horizontalalignment = "Left" verticalalignment = "TOP" width = "90" Height = "80" fontsize = "50"/> <textbox X: name = "tbcz" grid. column = "1" grid. row = "5" width = "300" Height = "80" horizontalalignment = "Left" verticalignment = "TOP"/> </GRID>

 

The effect is as follows:

 
Add two references before adding code to MainPage. xaml. cs.
One is Microsoft. Devices. Sensors (using the Accelerometer class)
The other is MicroSoft. Xna. Framework (mainly used in the Vector3 class)

 

// MainPage. xaml. cs code using System; using System. collections. generic; using System. linq; using System. net; using System. windows; using System. windows. controls; using System. windows. documents; using System. windows. input; using System. windows. media; using System. windows. media. animation; using System. windows. shapes; using Microsoft. phone. controls; using Microsoft. devices. sensors; namespace AccelerometerDemo {public Partial class MainPage: PhoneApplicationPage {Accelerometer acc; // Constructor public MainPage () {InitializeComponent (); if (! Accelerometer. isSupported) {MessageBox. show ("this device does not support accelerators");} else {acc = new Accelerometer (); acc. currentValueChanged + = new EventHandler <SensorReadingEventArgs <AccelerometerReading> (acc_CurrentValueChanged); // Microsoft recommends the CurrentValueChanged method to obtain the current coordinate value. Acc. readingChanged + = new EventHandler <AccelerometerReadingEventArgs> (acc_ReadingChanged); // This method is outdated, but can still be used with acc. start () ;}} void acc_ReadingChanged (object sender, AccelerometerReadingEventArgs e) {Deployment. current. dispatcher. beginInvoke () => ThreadSafeAccelerometerChanged (e);} void ThreadSafeAccelerometerChanged (AccelerometerReadingEventArgs e) {TBX. text = e. x. toString ("0.000"); TBY. text = e. y. toString ("0.000"); TBZ. text = e. z. toString ("0.000");} void acc_CurrentValueChanged (object sender, SensorReadingEventArgs <AccelerometerReading> e) {Deployment. current. dispatcher. beginInvoke () => ThreadSafeAccelerometerChanged (e);} void ThreadSafeAccelerometerChanged (SensorReadingEventArgs <AccelerometerReading> e) {// e. sensorReading. acceleration is the Vector3 class TBCX. text = Convert. toString (e. sensorReading. acceleration. x); TBCY. text = Convert. toString (e. sensorReading. acceleration. y); TBCZ. text = Convert. toString (e. sensorReading. acceleration. Z );}}}

 

Simulator result:
 

Both the simulator and the real machine pass the test.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.