1.新建项目在 xaml当中添加如下代码

  1. <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> 
  2.     <Button Content="Start" Height="72" HorizontalAlignment="Left" Margin="20,10,0,0" Name="startButton" VerticalAlignment="Top" Width="160" Click="startButton_Click"   /> 
  3.     <Button Content="Stop" Height="72" HorizontalAlignment="Right" Margin="0,10,20,0" Name="stopButton" VerticalAlignment="Top" Width="160" Click="stopButton_Click"    /> 
  4.     <TextBlock Height="30" HorizontalAlignment="Left"  Margin="20,100,0,0" Name="xTextBlock" Text="X: 1.0" VerticalAlignment="Top" Foreground="Red" FontSize="28" FontWeight="Bold"/> 
  5.     <TextBlock Height="30" HorizontalAlignment="Center"  Margin="0,100,0,0" Name="yTextBlock" Text="Y: 1.0" VerticalAlignment="Top" Foreground="Green" FontSize="28" FontWeight="Bold"/> 
  6.     <TextBlock Height="30" HorizontalAlignment="Right"  Margin="0,100,20,0" Name="zTextBlock" Text="Z: 1.0" VerticalAlignment="Top"  Foreground="Blue" FontSize="28" FontWeight="Bold"/> 
  7.     <Line x:Name="xLine" X1="240" Y1="350" X2="340" Y2="350" Stroke="Red" StrokeThickness="4"></Line> 
  8.     <Line x:Name="yLine" X1="240" Y1="350" X2="240" Y2="270" Stroke="Green" StrokeThickness="4"></Line> 
  9.     <Line x:Name="zLine" X1="240" Y1="350" X2="190" Y2="400" Stroke="Blue" StrokeThickness="4"></Line> 
  10.     <TextBlock Height="30" HorizontalAlignment="Center" Margin="6,571,6,0" Name="statusTextBlock" Text="TextBlock" VerticalAlignment="Top" Width="444" /> 
  11. </Grid> 

打开设计器 会得到如下界面 start 按钮为开启  加速传感器 , stop 停止加速传感器

 

3.添加Microsoft.Devices.Sensors 命名空间 到项目

 

4.点击确定 后打开cs 文件

 

  1. Accelerometer accelerometer; //初始化加速器 
  2.       // Constructor 
  3.       public MainPage() 
  4.       { 
  5.           InitializeComponent(); 
  6.           if (!Accelerometer.IsSupported)   //如果不支持 加速器 界面上的startButton,stopButton 不可用
  7.           { 
  8.               statusTextBlock.Text = "device do not support the accelerometer;"
  9.               startButton.IsEnabled = false
  10.               stopButton.IsEnabled = false
  11.           } 
  12.       
  13.       } 

 

5.添加 startButton 和stopButton 的click事件

 

  1. private void startButton_Click(object sender, RoutedEventArgs e) 
  2.     { 
  3.         accelerometer = new Accelerometer(); 
  4.         accelerometer.TimeBetweenUpdates = TimeSpan.FromMilliseconds(20); 
  5.         accelerometer.CurrentValueChanged += new EventHandler<SensorReadingEventArgs<AccelerometerReading>>(accelerometer_CurrentValueChanged); 
  6.         try 
  7.         { 
  8.             statusTextBlock.Text = "starting accelerometer"
  9.             accelerometer.Start(); 
  10.         } 
  11.         catch (Exception ex) 
  12.         { 
  13.             ex.ToString(); 
  14.             statusTextBlock.Text = "unable start the accelerometer"
  15.         } 
  16.     } 
  17.  
  18.   
  19.  
  20.     private void stopButton_Click(object sender, RoutedEventArgs e) 
  21.     { 
  22.         if (accelerometer != null) 
  23.         { 
  24.             // Stop the accelerometer. 
  25.             accelerometer.Stop(); 
  26.             statusTextBlock.Text = "accelerometer stopped."
  27.         } 
  28.     } 
  29. //该方法为回调方法,通过该方法可以获取到加速器的x,y,z轴的相关数据
  30.     void accelerometer_CurrentValueChanged(object sender, SensorReadingEventArgs<AccelerometerReading> e) 
  31.     { 
  32.         // Call UpdateUI on the UI thread and pass the AccelerometerReading. 
  33.         Dispatcher.BeginInvoke(() => UpdateUI(e.SensorReading)); 
  34.     } 

6.最后我们把更新UI界面数据的逻辑写入一个封装到一个方法里

 

  1. /// <summary> 
  2.        /// 更新ui数据 
  3.        /// </summary> 
  4.        /// <param name="accelerometerReading"></param> 
  5.        private void UpdateUI(AccelerometerReading accelerometerReading) 
  6.        { 
  7.            statusTextBlock.Text = "getting data"
  8.  
  9.            Vector3 acceleration = accelerometerReading.Acceleration; 
  10.  
  11.            // Show the numeric values. 
  12.            xTextBlock.Text = "X: " + acceleration.X.ToString("0.00"); 
  13.            yTextBlock.Text = "Y: " + acceleration.Y.ToString("0.00"); 
  14.            zTextBlock.Text = "Z: " + acceleration.Z.ToString("0.00"); 
  15.  
  16.            // Show the values graphically. 
  17.            xLinexLine.X2 = xLine.X1 + acceleration.X * 200; 
  18.            yLineyLine.Y2 = yLine.Y1 - acceleration.Y * 200; 
  19.            zLinezLine.X2 = zLine.X1 - acceleration.Z * 100; 
  20.            zLinezLine.Y2 = zLine.Y1 + acceleration.Z * 100; 
  21.        }