star870126 发表于 2015-5-10 12:04:24

Windows Phone 7(accelerometer)重力感应编程

  使用重力感应器accelerometer,需要引用类库Microsoft.Devices.Sensors ,所以需要在WMAppManifest.xml
加上



代码




using System;
using System.Security;
namespace Microsoft.Devices.Sensors
{
    // Summary:
    //   Provides Windows?Phone applications access to the device’s accelerometer
    //   sensor.
    public sealed class Accelerometer : IDisposable
    {
      // Summary:
      //   Creates a new instance of the Microsoft.Devices.Sensors.Accelerometer object.
      
      public Accelerometer();
      // Summary:
      //   Gets the current state of the accelerometer. The value is a member of the
      //   Microsoft.Devices.Sensors.SensorState enumeration.
      //
      // Returns:
      //   Type Microsoft.Devices.Sensors.SensorState.
      public SensorState State { get; }
      // Summary:
      //   Occurs when new data arrives from the accelerometer.
      public event EventHandler ReadingChanged;
      // Summary:
      //   Releases the managed and unmanaged resources used by the Microsoft.Devices.Sensors.Accelerometer.
      
      public void Dispose();
      //
      // Summary:
      //   Starts data acquisition from the accelerometer.
      
      public void Start();
      //
      // Summary:
      //   Stops data acquisition from the accelerometer.
      
      public void Stop();
    }
}

  X轴表示左右方向的重力大小
  Y轴表示上下方向的重力大小
  Z轴表示屏幕正上方下面的的重力大小
  实例


代码




MainPage.xaml

   
   
      
            
            
      
      
      
            
            
      
      
      
            
      
   


代码




MainPage.xaml.cs
using System;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Devices.Sensors;
using Microsoft.Phone.Controls;
namespace SilverlightAccelerometer
{
    public partial class MainPage : PhoneApplicationPage
    {
      public MainPage()
      {
            InitializeComponent();
            Accelerometer acc = new Accelerometer();//初始化一个重力感应器的类
            acc.ReadingChanged += OnAccelerometerReadingChanged;//触发重力感应的事件

            try
            {
                acc.Start();//开始加速计重力感应
            }
            catch (Exception exc)
            {
                txtblk.Text = exc.Message;
            }
      }
      void OnAccelerometerReadingChanged(object sender, AccelerometerReadingEventArgs args)
      {
            string str = String.Format("X = {0:F2}\n" +   //x轴表示屏幕的左右
                                       "Y = {1:F2}\n" +      //y轴表示屏幕的上下
                                       "Z = {2:F2}\n\n" +    //z轴表示屏幕正上方的上下
                                       "Magnitude = {3:F2}\n\n" +
                                       "{4}",
                                       args.X, args.Y, args.Z,
                                       Math.Sqrt(args.X * args.X + args.Y * args.Y +   //计算加速度
                                                                   args.Z * args.Z),                                       
                                       args.Timestamp);
            if (txtblk.CheckAccess())//判断线程是否允许访问
            {
                SetTextBlockText(txtblk, str);
            }
            else
            {
                //重新激活线程
                txtblk.Dispatcher.BeginInvoke(new SetTextBlockTextDelegate(SetTextBlockText),
                                              txtblk, str);
            }
      }
      delegate void SetTextBlockTextDelegate(TextBlock txtblk, string text);
      void SetTextBlockText(TextBlock txtblk, string text)
      {
            txtblk.Text = text;
      }
    }
}
页: [1]
查看完整版本: Windows Phone 7(accelerometer)重力感应编程