Windows Phone 7(accelerometer)重力感应编程

使用重力感应器accelerometer,需要引用类库Microsoft.Devices.Sensors ,所以需要在WMAppManifest.xml
加上
<Capability Name="ID_CAP_SENSORS" />

 

  
  
  
  
  1. 代码  
  2.  
  3.  
  4. using System;  
  5. using System.Security;  
  6.  
  7. namespace Microsoft.Devices.Sensors  
  8. {  
  9.     // Summary:  
  10.     //     Provides Windows?Phone applications access to the device’s accelerometer  
  11.     //     sensor.  
  12.     public sealed class Accelerometer : IDisposable  
  13.     {  
  14.         // Summary:  
  15.         //     Creates a new instance of the Microsoft.Devices.Sensors.Accelerometer object.  
  16.         [SecuritySafeCritical]  
  17.         public Accelerometer();  
  18.  
  19.         // Summary:  
  20.         //     Gets the current state of the accelerometer. The value is a member of the  
  21.         //     Microsoft.Devices.Sensors.SensorState enumeration.  
  22.         //  
  23.         // Returns:  
  24.         //     Type Microsoft.Devices.Sensors.SensorState.  
  25.         public SensorState State { get; }  
  26.  
  27.         // Summary:  
  28.         //     Occurs when new data arrives from the accelerometer.  
  29.         public event EventHandler<AccelerometerReadingEventArgs> ReadingChanged;  
  30.  
  31.         // Summary:  
  32.         //     Releases the managed and unmanaged resources used by the Microsoft.Devices.Sensors.Accelerometer.  
  33.         [SecuritySafeCritical]  
  34.         public void Dispose();  
  35.         //  
  36.         // Summary:  
  37.         //     Starts data acquisition from the accelerometer.  
  38.         [SecuritySafeCritical]  
  39.         public void Start();  
  40.         //  
  41.         // Summary:  
  42.         //     Stops data acquisition from the accelerometer.  
  43.         [SecuritySafeCritical]  
  44.         public void Stop();  
  45.     }  

 

 

X轴表示左右方向的重力大小

Y轴表示上下方向的重力大小

Z轴表示屏幕正上方下面的的重力大小

实例

 

  
  
  
  
  1. 代码  
  2.  
  3.  
  4. MainPage.xaml  
  5. <phone:PhoneApplicationPage   
  6.     x:Class="SilverlightAccelerometer.MainPage" 
  7.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  8.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
  9.     xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" 
  10.     xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" 
  11.     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  12.     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  13.     FontFamily="{StaticResource PhoneFontFamilyNormal}" 
  14.     FontSize="{StaticResource PhoneFontSizeNormal}" 
  15.     Foreground="{StaticResource PhoneForegroundBrush}" 
  16.     SupportedOrientations="PortraitOrLandscape" Orientation="Portrait" 
  17.     mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768" 
  18.     shell:SystemTray.IsVisible="True"> 
  19.  
  20.     <!--LayoutRoot contains the root grid where all other page content is placed--> 
  21.     <Grid x:Name="LayoutRoot" Background="Transparent"> 
  22.         <Grid.RowDefinitions> 
  23.             <RowDefinition Height="Auto"/> 
  24.             <RowDefinition Height="*"/> 
  25.         </Grid.RowDefinitions> 
  26.  
  27.         <!--TitlePanel contains the name of the application and page title--> 
  28.         <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28"> 
  29.             <TextBlock x:Name="ApplicationTitle" Text="SILVERLIGHT ACCELEROMETER" Style="{StaticResource PhoneTextNormalStyle}"/> 
  30.             <TextBlock x:Name="PageTitle" Text="main page" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/> 
  31.         </StackPanel> 
  32.  
  33.         <!--ContentPanel - place additional content here--> 
  34.         <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> 
  35.             <TextBlock Name="txtblk" 
  36.                         HorizontalAlignment="Center" 
  37.                         VerticalAlignment="Center" /> 
  38.         </Grid> 
  39.     </Grid> 
  40. </phone:PhoneApplicationPage> 

 

  
  
  
  
  1. 代码  
  2.  
  3.  
  4. MainPage.xaml.cs  
  5. using System;  
  6. using System.Windows;  
  7. using System.Windows.Controls;  
  8. using Microsoft.Devices.Sensors;  
  9. using Microsoft.Phone.Controls;  
  10.  
  11. namespace SilverlightAccelerometer  
  12. {  
  13.     public partial class MainPage : PhoneApplicationPage  
  14.     {  
  15.         public MainPage()  
  16.         {  
  17.             InitializeComponent();  
  18.  
  19.             Accelerometer acc = new Accelerometer();//初始化一个重力感应器的类  
  20.             acc.ReadingChanged += OnAccelerometerReadingChanged;//触发重力感应的事件  
  21.  
  22.             try  
  23.             {  
  24.                 acc.Start();//开始加速计重力感应  
  25.             }  
  26.             catch (Exception exc)  
  27.             {  
  28.                 txtblk.Text = exc.Message;  
  29.             }  
  30.         }  
  31.  
  32.         void OnAccelerometerReadingChanged(object sender, AccelerometerReadingEventArgs args)  
  33.         {  
  34.             string str = String.Format("X = {0:F2}\n" +     //x轴表示屏幕的左右    
  35.                                        "Y = {1:F2}\n" +      //y轴表示屏幕的上下   
  36.                                        "Z = {2:F2}\n\n" +    //z轴表示屏幕正上方的上下   
  37.                                        "Magnitude = {3:F2}\n\n" +  
  38.                                        "{4}",   
  39.                                        args.X, args.Y, args.Z,   
  40.                                        Math.Sqrt(args.X * args.X + args.Y * args.Y +   //计算加速度  
  41.                                                                    args.Z * args.Z),                                         
  42.                                        args.Timestamp);  
  43.  
  44.             if (txtblk.CheckAccess())//判断线程是否允许访问  
  45.             {  
  46.                 SetTextBlockText(txtblk, str);  
  47.             }  
  48.             else  
  49.             {  
  50.                 //重新激活线程  
  51.                 txtblk.Dispatcher.BeginInvoke(new SetTextBlockTextDelegate(SetTextBlockText),   
  52.                                               txtblk, str);  
  53.             }  
  54.         }  
  55.  
  56.         delegate void SetTextBlockTextDelegate(TextBlock txtblk, string text);  
  57.  
  58.         void SetTextBlockText(TextBlock txtblk, string text)  
  59.         {  
  60.             txtblk.Text = text;  
  61.         }  
  62.     }  

 

 

 

 

你可能感兴趣的:(编程,windows,phone,ACCELEROMETER,重力感应,7)