WP利用Accelerometer类制作加速度测量器。

MainPage.xaml中部分代码:
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
           <TextBlock VerticalAlignment="Top" Text="x:" Name="XLabel" Style="{StaticResource PhoneTextNormalStyle}" Margin="42,124,386,0"></TextBlock>
           <TextBlock VerticalAlignment="Top" Text=" " Name="XTextBlock" Style="{StaticResource PhoneTextExtraLargeStyle}" Margin="86,100,20,0" Foreground="{StaticResource PhoneAccentBrush}"></TextBlock>
           <TextBlock VerticalAlignment="Top" Text="y:" Name="YLabel" Style="{StaticResource PhoneTextNormalStyle}" Margin="42,236,386,0"></TextBlock>
           <TextBlock VerticalAlignment="Top" Text=" " Name="YTextBlock" Style="{StaticResource PhoneTextExtraLargeStyle}" Margin="86,212,52,0" Foreground="{StaticResource PhoneAccentBrush}"></TextBlock>
           <TextBlock VerticalAlignment="Top" Text="z:" Name="ZLabel" Style="{StaticResource PhoneTextNormalStyle}" Margin="42,332,386,0"></TextBlock>
           <TextBlock VerticalAlignment="Top" Text=" " Name="ZTextBlock" Style="{StaticResource PhoneTextExtraLargeStyle}" Margin="86,308,54,0"  Foreground="{StaticResource PhoneAccentBrush}"></TextBlock>
           <TextBlock VerticalAlignment="Top" Text="Total:" Name="TotalLabel" Style="{StaticResource PhoneTextNormalStyle}" Margin="24,420,386,0"></TextBlock>
           <TextBlock VerticalAlignment="Top" Text=" " Name="TotalTextBlock" Style="{StaticResource PhoneTextExtraLargeStyle}" Margin="86,400,54,0"  Foreground="{StaticResource PhoneAccentBrush}"></TextBlock>
           <TextBlock VerticalAlignment="Top" Text="status:" Name="statusLabel" Style="{StaticResource PhoneTextNormalStyle}" Margin="24,11,0,0" HorizontalAlignment="Left" Width="72" />
           <TextBlock VerticalAlignment="Top" Text="accelerometer stopped" Name="statusTextBlock" Style="{StaticResource PhoneTextNormalStyle}" Foreground="{StaticResource PhoneAccentBrush}" Margin="102,11,6,0" />
             
       </Grid>

MainPage.cs中部分代码:

建立一个Accelerometer对象accelerometer:

Accelerometer accelerometer;
在mainpage的构造函数添加一个applicationBar并为之添加一个控制加速器开关的按钮:
public MainPage()
        {
            InitializeComponent();
  
            ApplicationBar = new ApplicationBar();
            ApplicationBar.IsVisible = true;
  
            ApplicationBarIconButton startStopButton = new ApplicationBarIconButton(new Uri("/Images/startstop.png", UriKind.Relative));
            startStopButton.Text = "on/off";
            startStopButton.Click += new EventHandler(startStopButton_Click);
            ApplicationBar.Buttons.Add(startStopButton);
        }

为按钮添加事件处理函数:(强调一下,在老版本中ReadingChanged事件是加速度器数值改变时执行的事件,而新版本已改用CurrentValueChanged事件,因此我用了CurrentValueChanged事件处理)。  


void startStopButton_Click(object sender, EventArgs e)
        {
            // 构造并开始
            if (accelerometer == null)
            {
                accelerometer = new Accelerometer();
  
                //老版本加速度改变事件
                //accelerometer.ReadingChanged += new EventHandler<AccelerometerReadingEventArgs>(accelerometer_ReadingChanged);
                //wp8加速度改变事件
                accelerometer.CurrentValueChanged += accelerometer_CurrentValueChanged;
                  
                try
                {
                    statusTextBlock.Text = "starting accelerometer";
                    accelerometer.Start();
                }
                catch (AccelerometerFailedException exception)
                {
                    statusTextBlock.Text = "error starting accelerometer";
                }
            }
            else
            {
                try
                {
                    accelerometer.Stop();
                    accelerometer = null;
                    statusTextBlock.Text = "accelerometer stopped";
                }
                catch (AccelerometerFailedException exception)
                {
                    statusTextBlock.Text = "error stopping accelerometer";
                }
  
            }
        }
实现委托函数,这里设计到UI线程访问问题,在此不做过多解释。
void accelerometer_CurrentValueChanged(object sender, SensorReadingEventArgs<AccelerometerReading> e)
       {
           Deployment.Current.Dispatcher.BeginInvoke(() => ChangeUI(e));
       }

完成ChangeUI函数(这里记得要引用程序集Microsoft.Xna.Framework):  

void ChangeUI(SensorReadingEventArgs<AccelerometerReading> e)
        {
            if (accelerometer != null)
            {
                statusTextBlock.Text = accelerometer.State.ToString();
                Vector3 v3 = e.SensorReading.Acceleration;
                    
                XTextBlock.Text = v3.X.ToString("0.00");
                YTextBlock.Text = v3.Y.ToString("0.00");
                ZTextBlock.Text = v3.Z.ToString("0.00");
    
                TotalTextBlock.Text = v3.Length().ToString();
            }
        }

ok,加速度器完成。

猜你喜欢

转载自blog.csdn.net/lwcbest/article/details/38088953