wp7上加速器例子講解(官方demo)

 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.        } 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章