当前位置: 移动技术网 > IT编程>移动开发>WP > windows phone加速计

windows phone加速计

2018年10月03日  | 移动技术网IT编程  | 我要评论

女衬衫品牌,汽油涨价最新消息,南通邮编

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

首先是命名空间的引用:

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



摘自  神舟龙

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网