windows phone 加速计(5)

在windows phone 中存在着加速计,我们可以利用加速计获得用户手机的状态,根据手机状态调整我们的程序,这样会更人性化;windows phone 加速计采用的是三轴坐标定位即在三维空间中的坐标,加速计在三维空间中的点(x,y,z)是矢量的,包含大小和方向,方向就是从原点(0,0,0)到三维空间中的点(x,y,z),矢量的大小则是毕达格斯定理(貌似是高中有学到过),公式为√a^2+b^2+c^2;加速计原理详细见http://www.cnblogs.com/liuq0s/archive/2010/09/14/1825908.html

首先是命名空间的引用:

//引用
//Accelerometer类用到
using Microsoft.Devices.Sensors;
//Dispatcher类用到
using System.Windows.Threading;

 

在xaml文件中定义一个textblock 用于显示一个点的坐标,矢量的大小和时间戳:

<TextBlock x:Name="txtCoordinates" Text="显示三轴坐标" HorizontalAlignment="Center" VerticalAlignment="Center"  Grid.Row="1"></TextBlock>

 隐藏代码文件如下:

View Code
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;
//引用
//Accelerometer类用到
using Microsoft.Devices.Sensors;
//Dispatcher类用到
using System.Windows.Threading;

namespace GetAccelerometerCoordinates
{
    public partial class MainPage : PhoneApplicationPage
    {
        // 构造函数
        public MainPage()
        {
            InitializeComponent();
            //实例化加速计--知识点①
            Accelerometer acc = new Accelerometer();
            //加速计值发生变化是发生--知识点②
            acc.ReadingChanged += new EventHandler<AccelerometerReadingEventArgs>(acc_ReadingChanged);
            try
            {
                //开始获取加速计数据
                acc.Start();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
           
        }
        //当加速计值改变时发生--知识点③
        void acc_ReadingChanged(object sender, AccelerometerReadingEventArgs e)
        {
            string str = "x:" + e.X + ";\nY:" + e.Y + ";\nZ:" + e.Z + ";\n矢量大小:" + Math.Sqrt(e.Z * e.Z + e.Y * e.Y + e.X * e.X) + ";\n时间戳:" + e.Timestamp;

            //确定嗲用线程是否可以访问此对象--知识点④
            if (txtCoordinates.CheckAccess())
            {
                txtCoordinates.Text = str;
            }
            else
            {
                //线程中异步实现txtCoordinates赋值--知识点⑤
                txtCoordinates.Dispatcher.BeginInvoke(new SetTextDel(SetText), txtCoordinates, str);
            }
        }
        //委托
        delegate void SetTextDel(TextBlock txtCoordinates, string str);
        //方法
        void SetText(TextBlock txtCoordinates, string str)
        {
            txtCoordinates.Text = str;
        }
    }
}

 知识点①:Accelerometer类提供了访问加速计的访问

   属性:

   

State

加速計狀態,為枚舉類型,在使用start方法之前可先獲取加速計狀態,看加速計是否可用

CurrentValue

獲得加速計,羅盤,的相關數據,

IsDataValid

获取传感器数据的有效性,true為有效,false為無效

IsSupported

應用程序是否支持加速計,支持则为 true;否则为 false

TimeBetweenUpdates

獲取CurrentValueChanged 事件之间的首选时间

  知识点ReadingChanged事件在windows phone os 7.1中已过期,建议使用CurrentValueChanged使用方法和ReadingChanged方法类似

  知识点③ :该事件中的传递的参数,通过该参数可以获得在三维空间中的坐标,并可根据坐标值进行运算,如获得矢量值

  知识点④:CheckAccess方法表示是否可以访问UI线程,如果可以我们可以直接赋值textblock,如果不可以就的跨线程操作

  知识点⑤:Dispatcher类用于管理线程工作项队列,其中BeginInvoke(Delegate, Object())方法用于线程上的指定参数数组以异步方式执行指定委托,这里定义的委托和方法参数一致

 

效果图:此效果图是在模拟器中的坐标,在模拟器中的坐标会一直是这样,不会改变,可以在windows phone手机中获得真是的数值

 小结:加速计是获取外部信息的设备,除此之外windows phone还有定位服务, 个人理解手机在三维空间的中的坐标,类似于对重力的一种分解,还望高手指点迷津

 

 

 

 

 

 

 

 

 

 

 

 

 

posted @ 2012-04-10 11:24  神舟龙  阅读(1367)  评论(1编辑  收藏  举报