天天看點

windows phone 加速計

首先是命名空間的引用:

//引用

//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;

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;

    }

}

   屬性:

   

<a href="http://msdn.microsoft.com/zh-cn/library/microsoft.devices.sensors.accelerometer.state%28v=vs.92%29.aspx">State</a>

<a href="http://msdn.microsoft.com/zh-cn/library/hh239261%28v=vs.92%29.aspx">CurrentValue</a>

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

<a href="http://msdn.microsoft.com/zh-cn/library/hh220799%28v=vs.92%29.aspx">IsDataValid</a>

擷取傳感器資料的有效性,true為有效,false為無效

<a href="http://msdn.microsoft.com/zh-cn/library/microsoft.devices.sensors.accelerometer.issupported%28v=vs.92%29.aspx">IsSupported</a>

應用程式是否支援加速計,支援則為 true;否則為 false

<a href="http://msdn.microsoft.com/zh-cn/library/hh220884%28v=vs.92%29.aspx">TimeBetweenUpdates</a>

獲取CurrentValueChanged 事件之間的首選時間

  知識點③ :該事件中的傳遞的參數,通過該參數可以獲得在三維空間中的坐标,并可根據坐标值進行運算,如獲得矢量值

  知識點④:CheckAccess方法表示是否可以通路UI線程,如果可以我們可以直接指派textblock,如果不可以就的跨線程操作

效果圖:此效果圖是在模拟器中的坐标,在模拟器中的坐标會一直是這樣,不會改變,可以在windows phone手機中獲得真是的數值

 小結:加速計是擷取外部資訊的裝置,除此之外windows phone還有定位服務, 個人了解手機在三維空間的中的坐标,類似于對重力的一種分解,還望高手指點迷津

windows phone 加速計

本文轉自shenzhoulong  51CTO部落格,原文連結:http://blog.51cto.com/shenzhoulong/830577,如需轉載請自行聯系原作者

繼續閱讀