There is an accelerator in windows phone. We can use the accelerator to get the user's mobile phone status and adjust our programs based on the mobile phone status, which will be more user-friendly; the windows phone accelerator uses three-axis coordinate positioning, that is, coordinates in three-dimensional space. The points x, y, and z of the accelerator in three-dimensional space are vector, including the size and direction, the direction is from the point 0, 0, 0) to the point x, y, z in the three-dimensional space. The vector size is the bidargus theorem, which seems to have been learned by High School ), the formula is √ a ^ 2 + B ^ 2 + c ^ 2. for the principle of the accelerator, see the http://www.cnblogs.com/liuq0s/archive/2010/09/14/1825908.html
First, reference the namespace:
// Reference
// Used by the Accelerometer class
Using Microsoft. Devices. Sensors;
// Used by the Dispatcher class
Using System. Windows. Threading;
650) this. width = 650; "width =" 577 "height =" 394 "src =" http://www.bkjia.com/uploads/allimg/131228/0T914J07-0.png "alt =" "/>
Define a textblock In The xaml file to display the coordinates of a vertex, the vector size and timestamp:
<TextBlock x: Name = "txtCoordinates" Text = "show three-axis coordinates" HorizontalAlignment = "Center" VerticalAlignment = "Center" Grid. Row = "1"> </TextBlock>
The hidden code file is as follows:
650) this. width = 650; "style =" "id =" code_img_opened_375ee2bc-7261-4211-b965-adb4d3e1c263 "class =" code_img_opened "src =" http://www.bkjia.com/uploads/allimg/131228/0T914DR-1.gif "alt =" "/> View 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;
// Reference
// Used by the Accelerometer class
Using Microsoft. Devices. Sensors;
// Used by the Dispatcher class
Using System. Windows. Threading;
Namespace GetAccelerometerCoordinates
{
Public partial class MainPage: PhoneApplicationPage
{
// Constructor
Public MainPage ()
{
InitializeComponent ();
// Instantiate the accelerator-knowledge point ①
Accelerometer acc = new Accelerometer ();
// When the value of the accelerator changes, it is the occurrence of the change-knowledge point ②
Acc. ReadingChanged + = new EventHandler <AccelerometerReadingEventArgs> (acc_ReadingChanged );
Try
{
// Start to obtain the accelerated counting data
Acc. Start ();
}
Catch (Exception ex)
{
MessageBox. Show (ex. Message );
}
}
// When the value of acceleration changes-knowledge point ③
Void acc_ReadingChanged (object sender, AccelerometerReadingEventArgs e)
{
String str = "x:" + e. X + "; \ nY:" + e. Y + "; \ nZ:" + e. Z + "; \ n vector size:" + Math. sqrt (e. Z * e. Z + e. Y * e. Y + e. X * e. x) + "; \ n timestamp:" + e. timestamp;
// Determine whether the producer thread can access this object-knowledge point 4
If (txtCoordinates. CheckAccess ())
{
TxtCoordinates. Text = str;
}
Else
{
// Assign txtCoordinates values asynchronously in the thread-knowledge point ⑤
TxtCoordinates. Dispatcher. BeginInvoke (new SetTextDel (SetText), txtCoordinates, str );
}
}
// Delegate
Delegate void SetTextDel (TextBlock txtCoordinates, string str );
// Method
Void SetText (TextBlock txtCoordinates, string str)
{
TxtCoordinates. Text = str;
}
}
} Copy the code
Knowledge Point ①: the Accelerometer class provides access to the access accelerator
Attribute:
| State |
Accelerator compute. It is a compute type. before using the start method, you can obtain the accelerator compute to see if the accelerator design is available. |
| CurrentValue |
Correlation data of dynamic acceleration, continuous acceleration, |
| IsDataValid |
Obtains the validity of sensor data. Valid values are true, and invalid values are false. |
| IsSupported |
Whether the application supports the Acceleration Program. If the application supports acceleration, the value true; otherwise, the value false. |
| TimeBetweenUpdates |
Returns the preferred time between CurrentValueChanged events. |
Knowledge Point ②: The ReadingChanged event has expired in windows phone OS 7.1. We recommend that you use CurrentValueChanged in a similar way to ReadingChanged.
Knowledge Point ③: The parameter passed in the event. With this parameter, coordinates in 3D space can be obtained, and operations can be performed based on coordinate values, such as obtaining vector values.
Knowledge Point ④: The CheckAccess method indicates whether the UI thread can be accessed. If yes, textblock can be assigned directly. If not, cross-thread operations can be performed.
Knowledge Point ⑤: the Dispatcher class is used to manage the thread work item queue. The BeginInvoke (Delegate, Object () method is used to run the specified Delegate asynchronously in the specified parameter array on the thread, the delegate and method parameters defined here are consistent.
650) this. width = 650; "width =" 342 "height =" 548 "border =" 0 "src =" http://www.bkjia.com/uploads/allimg/131228/0T914K24-2.png "alt =" "/>
: This is the coordinates in the simulator. The coordinates in the simulator will always be like this and will not change. You can get the true value on the windows phone.
Summary: The accelerator is a device that obtains external information. In addition, there is a location service for windows phone. I personally understand the coordinates of mobile phones in three-dimensional space, which is similar to a decomposition of gravity, also look at high finger points maze 650) this. width = 650; "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/0T914C37-3.gif "/>
This article from the "Shenzhou Dragon" blog, please be sure to keep this source http://shenzhoulong.blog.51cto.com/1191789/830577