关闭

<WP7>window phone 加速器(重力感应)初体验

1859人阅读 评论(0) 收藏 举报

从我的网易博客移过来的。。。。


WP7的加速感应器。也就是日常所说的实现重力感应的模块。Windows Phone

设备中的加速度感应器是用来度量3个坐标轴加速度的装置。大致坐标如下:


 

 我们所需要做的就是从Accelerometer对象中读取到相应的数据。

 

//MainPage.xaml的代码主要是在ContentPanel中加入以下代码
<Grid.ColumnDefinitions>
                <ColumnDefinition Width="100"/>
                <ColumnDefinition Width="300" />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="100"/>
                <RowDefinition Height="100"/>
                <RowDefinition Height="100"/>
                <RowDefinition Height="100"/>
                <RowDefinition Height="100"/>
                <RowDefinition Height="100"/>
            </Grid.RowDefinitions>
            <TextBlock Text="X:" Grid.Column="0" Grid.Row="0" Width="88"  Height="82"
                       HorizontalAlignment="Left"
                  VerticalAlignment="Top" FontSize="50"    />
            <TextBox x:Name="TBX" Width="300" Height="80" Grid.Column="1" 
                     HorizontalAlignment="Left" VerticalAlignment="Top" Grid.Row="0" />
            <TextBlock Text="Y:" Grid.Row="1" Grid.Column="0" HorizontalAlignment="Left"
                       VerticalAlignment="Top" Width="90" Height="80" FontSize="50"/>
            <TextBox x:Name="TBY" Grid.Row="1" Grid.Column="1" HorizontalAlignment="Left"
                     VerticalAlignment="Top" Width="300" Height="80"/>
            <TextBlock Text="Z:" Grid.Column="0" Grid.Row="2" HorizontalAlignment="Left"
                       VerticalAlignment="Top" Width="90" Height="80" FontSize="50"/>
            <TextBox x:Name="TBZ" Grid.Column="1" Grid.Row="2" Width="300" Height="80"
                     HorizontalAlignment="Left" VerticalAlignment="Top"/>
            <TextBlock Text="CX:" Grid.Column="0" Grid.Row="3" Width="88"  Height="82"
                       HorizontalAlignment="Left"
                  VerticalAlignment="Top" FontSize="50"    />
            <TextBox x:Name="TBCX" Width="300" Height="80" Grid.Column="1" 
                     HorizontalAlignment="Left" VerticalAlignment="Top" Grid.Row="3" />
            <TextBlock Text="CY:" Grid.Row="4" Grid.Column="0" HorizontalAlignment="Left"
                       VerticalAlignment="Top" Width="90" Height="80" FontSize="50"/>
            <TextBox x:Name="TBCY" Grid.Row="4" Grid.Column="1" HorizontalAlignment="Left"
                     VerticalAlignment="Top" Width="300" Height="80"/>
            <TextBlock Text="CZ:" Grid.Column="0" Grid.Row="5" HorizontalAlignment="Left"
                       VerticalAlignment="Top" Width="90" Height="80" FontSize="50"/>
            <TextBox x:Name="TBCZ" Grid.Column="1" Grid.Row="5" Width="300" Height="80"
                     HorizontalAlignment="Left" VerticalAlignment="Top"/>
        </Grid>

效果如下:

 
在MainPage.xaml.cs添加代码前,先添加两个引用。
一个是Microsoft.Devices.Sensors (使用Accelerometer类)
另一个是MicroSoft.Xna.Framework (主要用到的是Vector3类)

//MainPage.xaml.cs代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Devices.Sensors;
namespace AccelerometerDemo
{
    public partial class MainPage : PhoneApplicationPage
    {
        Accelerometer acc;
        // Constructor
        public MainPage()
        {
            InitializeComponent();
            if (!Accelerometer.IsSupported)
            {
                MessageBox.Show("该设备不支持加速器");
            }
            else
            {
                acc = new Accelerometer();
                acc.CurrentValueChanged += new EventHandler<SensorReadingEventArgs<AccelerometerReading>>(acc_CurrentValueChanged);
//微软推荐的是这个CurrentValueChanged方法来获取当前的坐标值。
                acc.ReadingChanged += new EventHandler<AccelerometerReadingEventArgs>(acc_ReadingChanged);//这个方法已经过时,但是还是能用
                acc.Start();
            }  
        }
        void acc_ReadingChanged(object sender, AccelerometerReadingEventArgs e)
        {
            Deployment.Current.Dispatcher.BeginInvoke(() => ThreadSafeAccelerometerChanged(e));
        }
        void ThreadSafeAccelerometerChanged(AccelerometerReadingEventArgs e)
        {
            TBX.Text = e.X.ToString("0.000");
            TBY.Text = e.Y.ToString("0.000");
            TBZ.Text = e.Z.ToString("0.000");
        }
        void acc_CurrentValueChanged(object sender, SensorReadingEventArgs<AccelerometerReading> e)
        {
            Deployment.Current.Dispatcher.BeginInvoke(() => ThreadSafeAccelerometerChanged(e));
        }
        void ThreadSafeAccelerometerChanged(SensorReadingEventArgs<AccelerometerReading> e)
        {
     //e.SensorReading.Acceleration就是Vector3类
            TBCX.Text = Convert.ToString(e.SensorReading.Acceleration.X);
            TBCY.Text = Convert.ToString(e.SensorReading.Acceleration.Y);
            TBCZ.Text = Convert.ToString(e.SensorReading.Acceleration.Z);
        }
        
    }
}


 模拟器结果:
模拟器与真机都测试通过
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:276763次
    • 积分:2993
    • 等级:
    • 排名:第11700名
    • 原创:62篇
    • 转载:6篇
    • 译文:0篇
    • 评论:136条
    我的其他博客
    最新评论