Different windows Phone (18)

Source: Internet
Author: User
Tags thread time interval xmlns

Device (equipment) acceleration sensor, digital compass sensor

Introduced

Unique device for Windows Phone 7.5 (SDK 7.1)

Acceleration Sensor (Accelerometer)

Digital Compass (magnetometer)

Example

1, demonstrating how to use the acceleration sensor

Accelerometerdemo.xaml

<phone:phoneapplicationpage x:class= "Demo.Device.AccelerometerDemo" xmlns= "Http://schemas.microsoft.com/winf X/2006/xaml/presentation "xmlns:x=" Http://schemas.microsoft.com/winfx/2006/xaml "xmlns:phone=" Clr-namespace: Microsoft.phone.controls;assembly=microsoft.phone "Xmlns:shell=" clr-namespace:microsoft.phone.shell;assembly= Microsoft.phone "xmlns:d=" http://schemas.microsoft.com/expression/blend/2008 "xmlns:mc=" http:// schemas.openxmlformats.org/markup-compatibility/2006 "fontfamily=" {StaticResource PhoneFontFamilyNormal} "FontSiz E= ' {StaticResource phonefontsizenormal} ' foreground= ' {StaticResource Phoneforegroundbrush} ' SupportedOrientations= "Portrait" orientation= "Portrait" mc:ignorable= "D" d:designheight= "768" d:designwidth= "The Shell:SystemTray.IsVis" ible= "True" > <grid x:name= "layoutroot" background= "Transparent" > <stackpanel orientation= "Ve Rtical "> <textblock Name= "lblaccelerometersupported"/> <button name= "btnstart" content= "open acceleration Sensor" click= "btnStart_Click"/> <button name= "Btnstop" content= "Turn off acceleration sensor" click= "Btnstop_click"/> <textblock-name= "lb Laccelerometerstatus "/> <textblock name=" lbltimebetweenupdates "/> <textblock Name= "Lblmsg"/> </StackPanel> </Grid> </phone:PhoneApplicationPage>

AccelerometerDemo.xaml.cs

* * Demonstrates how to use the accelerometer * * Accelerometer-to access the accelerometer * issupported in the device-whether the device supports an accelerometer * isdatavalid-whether it can be accelerated from CurrentValue-Accelerometer Current data, accelerometerreading type * timebetweenupdates-trigger Currentvaluechange in the degree sensor The time interval for the D event, if the value set is less than the minimum allowable value for accelerometer, the value of this property will be set to the current state of the minimum allowable value of the accelerometer (Microsoft.Devices.Sens Ors. sensorstate enum) * notsupported-device does not support acceleration sensor * Ready-Accelerometer is ready and parsing data * initializing-      Acceleration Sensor Initializing * NoData-Accelerometer cannot get data * nopermissions-no permission call Acceleration sensor * Disabled-acceleration sensor disabled * Start ()-Turn on the accelerometer * STOP ()-Turn off the accelerometer * currentvaluechanged-the event triggered by the change in the data acquired by the accelerometer, properties timebetweenupdates Value determines the time interval for triggering this event * * accelerometerreading-Accelerometer data * acceleration-detail data, Vector3 type value * DATETIMEOFFSE T-the point at which data is obtained from the accelerometer * * * * * The interpretation of X Y Z in the value of the Vector3 type obtained from the accelerometer is as follows * Mobile coordinate system: Take the mobile phone position as reference, assuming the mobile phone vertical horizontal plane (vertical), screen The curtain is on you, then * 1, right and left is XAxis, the right side is positive direction, the left side is negative direction * 2, up and down is the Y axis, the upper side is positive direction, the lower side is negative direction * 3, inside and outside is the Z axis, close to you in the positive direction, away from you for negative direction * Above can be used relative to the phone position of the right-hand coordinate system to understand * X Y Z value of the center point to the ground plane direction 
The cosine/using System of the angle between the line and each corresponding axis positive direction; 
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; 
   
Using Microsoft.Xna.Framework; namespace Demo.device {public partial class Accelerometerdemo:phoneapplicationpage {private Accele 
   
        Rometer _accelerometer; 
   
            Public Accelerometerdemo () {InitializeComponent (); Determine if the device supports acceleration sensor if (accelerometer.issupported) {Lblaccelerometerstatus.text 
            = "This device supports acceleration sensor"; 
 } else {               Lblaccelerometerstatus.text = "This device does not support acceleration sensor"; 
                btnstart.isenabled = false; 
            btnstop.isenabled = false; }} private void btnStart_Click (object sender, RoutedEventArgs e) {if (_accel Erometer = = null) {//instantiated accelerometer, registering related events _accelerometer = new Accele 
                Rometer (); _accelerometer. 
                Timebetweenupdates = Timespan.frommilliseconds (1); _accelerometer. Currentvaluechanged + = new Eventhandler<sensorreadingeventargs<accelerometerreading>> (_accelerometer_ 
   
                currentvaluechanged); Lbltimebetweenupdates.text = "Timebetweenupdates set to 1 milliseconds, actually" + _accelerometer. 
            TimeBetweenUpdates.TotalMilliseconds.ToString () + "millisecond"; try {//Open acceleration sensor _accelerometer. 
                Start (); LblaccelerometerStatus.text = "acceleration sensor is turned on"; 
                The catch (Exception ex) {lblaccelerometerstatus.text = "The acceleration sensor has failed to open"; MessageBox.Show (ex. 
            ToString ()); }} private void Btnstop_click (object sender, RoutedEventArgs e) {if (_accele Rometer!= null) {//close acceleration sensor _accelerometer. 
                Stop (); 
            Lblaccelerometerstatus.text = "acceleration sensor is closed"; } void _accelerometer_currentvaluechanged (object sender, Sensorreadingeventargs<accelerometerre Ading> e) {//Note: This method is run in a background thread, so you need to update the UI note to invoke the UI thread Dispatcher.begininvoke (() =&gt ; 
        UpdateUI (e.sensorreading)); 
            //update UI private void UpdateUI (Accelerometerreading accelerometerreading) { 
   
            Vector3 acceleration = accelerometerreading.acceleration; //output X Y Z value Lblmsg.text = "acceleration." X: "+ acceleration." 
            X.tostring ("0.0"); 
            Lblmsg.text + = Environment.NewLine; Lblmsg.text = "acceleration." Y: "+ acceleration." 
            Y.tostring ("0.0"); 
            Lblmsg.text + = Environment.NewLine; Lblmsg.text = "acceleration." Z: "+ acceleration." 
        Z.tostring ("0.0"); } 
    } 
}

Related Article

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.