WP利用Accelerometer类制作加速度测量器。
来源:互联网 发布:常用linux命令 编辑:程序博客网 时间:2024/04/28 02:36
MainPage.xaml中部分代码:
为按钮添加事件处理函数:(强调一下,在老版本中ReadingChanged事件是加速度器数值改变时执行的事件,而新版本已改用CurrentValueChanged事件,因此我用了CurrentValueChanged事件处理)。
完成ChangeUI函数(这里记得要引用程序集Microsoft.Xna.Framework):
ok,加速度器完成。
<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,加速度器完成。
0 0
- WP利用Accelerometer类制作加速度测量器。
- phoneGap得到加速度Accelerometer
- 加速度传感器Accelerometer
- 时间间隔测量器类
- Windows Phone开发之Accelerometer-加速度传感器
- Android传感器API之:加速度Accelerometer功能源码
- H5+的使用-----设备的加速度管理Accelerometer模块
- wp主题制作
- wp学习之主题制作
- 加速度
- Android手机利用加速度统计跌落次数
- 标准体重测量器
- 标准体重测量器
- 制作wp主题遇到问题汇总
- wp开发 progress bar的制作
- Accelerometer sensor
- PhoneGap-----Accelerometer
- wp
- 一切成功源于积累——20140724 美瑞 上演疯狂-2
- linux下查看和修改文件编码格式
- android 五种存储
- 英语,从零开始
- C语言函数参数的传递详解
- WP利用Accelerometer类制作加速度测量器。
- 开源项目工作进展(一)
- CSS选择器的优化
- UIView详解
- 线程函数有关的问题
- 7.24 心得
- Ubuntu下配置Nodejs,并安装express和socket.io
- Netty4-- ByteBuf和设计模式
- 一竿清雨