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還有定位服務, 個人理解手機在三維空間的中的座標,類似於對重力的一種分解,還望高手指點迷津

來源:http://www.th7.cn/Program/wp7/2012/04/10/69099.shtml

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章