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 coordinates to locate the coordinates in three-dimensional space. The points (x, y, z) in the three-dimensional space are vector, including the size and direction, the direction is the point (x, y, z) from the origin point (0, 0) to the three-dimensional space, and the vector size is the bidargus theorem (which seems to have been learned in 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;
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:
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;
// quote
// 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 ();
// Instantiated accelerometer--knowledge points①
Accelerometer acc = new Accelerometer ();
// A change in accelerometer value is happening-knowledge point ②
acc.ReadingChanged + = new EventHandler <AccelerometerReadingEventArgs> (acc_ReadingChanged);
try
{
// Start to get accelerometer data
acc.Start ();
}
catch (Exception ex)
{
MessageBox.Show (ex.Message);
}
}
// Occurs when the accelerometer value changes-knowledge point ③
void acc_ReadingChanged (object sender, AccelerometerReadingEventArgs e)
{
string str = "x:" + eX + "; \ nY:" + eY + "; \ nZ:" + eZ + "; \ nVector size:" + Math.Sqrt (eZ * eZ + eY * eY + eX * eX) + "; \ nTimestamp:" + e.Timestamp;
// Determine whether the use thread can access this object-knowledge point ④
if (txtCoordinates.CheckAccess ())
{
txtCoordinates.Text = str;
}
else
{
// Asynchronous implementation of txtCoordinates assignment in threads--knowledge point ⑤
txtCoordinates.Dispatcher.BeginInvoke (new SetTextDel (SetText), txtCoordinates, str);
}
}
// Delegation
delegate void SetTextDel (TextBlock txtCoordinates, string str);
//method
void SetText (TextBlock txtCoordinates, string str)
{
txtCoordinates.Text = str;
}
}
}
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 yes, the value true; otherwise, the value False |
Timebetweenupdates |
Retrieve currentvaluechanged Preferred time between 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.
: 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 point maze