当前位置:编程学习 > wap >>

windows phone加速计

  
在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还有定位服务, 个人理解手机在三维空间的中的坐标,类似于对重力的一种分解,还望高手指点迷津



摘自  神舟龙

补充:移动开发 , Windows Phone ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,