与众不同windows phone (18)

Device(设备)之加速度传感器, 数字罗盘传感器

介绍

与众不同 windows phone 7.5 (sdk 7.1) 之设备

加速度传感器(加速度计)

数字罗盘(磁力计)

示例

1、演示如何使用加速度传感器

AccelerometerDemo.xaml

<phone:PhoneApplicationPage
    x:Class="Demo.Device.AccelerometerDemo"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480"
    shell:SystemTray.IsVisible="True"> 

    <Grid x:Name="LayoutRoot" Background="Transparent">
        <StackPanel Orientation="Vertical"> 

            <TextBlock Name="lblAccelerometerSupported" />
            <Button Name="btnStart" Content="打开加速度传感器" Click="btnStart_Click" />
            <Button Name="btnStop" Content="关闭加速度传感器" Click="btnStop_Click" />
            <TextBlock Name="lblAccelerometerStatus" />
            <TextBlock Name="lblTimeBetweenUpdates" />
            <TextBlock Name="lblMsg" /> 

        </StackPanel>
    </Grid> 

</phone:PhoneApplicationPage>

AccelerometerDemo.xaml.cs

/*
 * 演示如何使用加速度传感器
 *
 * Accelerometer - 用于访问设备中的加速度计
 *     IsSupported - 设备是否支持加速度传感器
 *     IsDataValid - 是否可从加速度传感器中获取到有效数据
 *     CurrentValue - 加速度传感器当前的数据,AccelerometerReading 类型
 *     TimeBetweenUpdates - 触发 CurrentValueChanged 事件的时间间隔,如果设置的值小于 Accelerometer 允许的最小值,则此属性的值将被设置为 Accelerometer 允许的最小值
 *     State - 加速度计的当前的状态(Microsoft.Devices.Sensors.SensorState 枚举)
 *         NotSupported - 设备不支持加速度传感器
 *         Ready - 加速度传感器已准备好,并且正在解析数据
 *         Initializing - 加速度传感器正在初始化
 *         NoData - 加速度传感器无法获取数据
 *         NoPermissions - 无权限调用加速度传感器
 *         Disabled - 加速度传感器被禁用
 *     Start() - 打开加速度计
 *     Stop() - 关闭加速度计
 *     CurrentValueChanged - 加速度传感器获取到的数据发生改变时所触发的事件,属性 TimeBetweenUpdates 的值决定触发此事件的时间间隔
 *
 * AccelerometerReading - 加速度传感器数据
 *     Acceleration - 详细数据,Vector3 类型的值
 *     DateTimeOffset - 从加速度传感器中获取到数据的时间点
 *
 *
 *
 * 关于从加速度传感器中获取到的 Vector3 类型的值中 X Y Z 的解释如下
 * 手机坐标系:以手机位置为参照,假设手机垂直水平面放(竖着放),屏幕对着你,那么
 * 1、左右是 X 轴,右侧为正方向,左侧为负方向
 * 2、上下是 Y 轴,上侧为正方向,下侧为负方向
 * 3、里外是 Z 轴,靠近你为正方向,远离你为负方向
 * 以上可以用相对于手机位置的右手坐标系来理解
 * X Y Z 的值为中心点到地平面方向的线与各个对应轴线正方向的夹角的余弦值
 */

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; 

using Microsoft.Devices.Sensors;
using Microsoft.Xna.Framework; 

namespace Demo.Device
{
    public partial class AccelerometerDemo : PhoneApplicationPage
    {
        private Accelerometer _accelerometer; 

        public AccelerometerDemo()
        {
            InitializeComponent(); 

            // 判断设备是否支持加速度传感器
            if (Accelerometer.IsSupported)
            {
                lblAccelerometerStatus.Text = "此设备支持加速度传感器";
            }
            else
            {
                lblAccelerometerStatus.Text = "此设备不支持加速度传感器"; 

                btnStart.IsEnabled = false;
                btnStop.IsEnabled = false;
            }
        } 

        private void btnStart_Click(object sender, RoutedEventArgs e)
        {
            if (_accelerometer == null)
            {
                // 实例化 Accelerometer,注册相关事件
                _accelerometer = new Accelerometer();
                _accelerometer.TimeBetweenUpdates = TimeSpan.FromMilliseconds(1);
                _accelerometer.CurrentValueChanged += new EventHandler<SensorReadingEventArgs<AccelerometerReading>>(_accelerometer_CurrentValueChanged); 

                lblTimeBetweenUpdates.Text = "TimeBetweenUpdates 设置为 1 毫秒,实际为 " + _accelerometer.TimeBetweenUpdates.TotalMilliseconds.ToString() + " 毫秒";
            } 

            try
            {
                // 打开加速度传感器
                _accelerometer.Start();
                lblAccelerometerStatus.Text = "加速度传感器已打开";
            }
            catch (Exception ex)
            {
                lblAccelerometerStatus.Text = "加速度传感器已打开失败";
                MessageBox.Show(ex.ToString());
            }
        } 

        private void btnStop_Click(object sender, RoutedEventArgs e)
        {
            if (_accelerometer != null)
            {
                // 关闭加速度传感器
                _accelerometer.Stop();
                lblAccelerometerStatus.Text = "加速度传感器已关闭";
            }
        } 

        void _accelerometer_CurrentValueChanged(object sender, SensorReadingEventArgs<AccelerometerReading> e)
        {
            // 注:此方法是在后台线程运行的,所以需要更新 UI 的话注意要调用 UI 线程
            Dispatcher.BeginInvoke(() => UpdateUI(e.SensorReading));
        } 

        // 更新 UI
        private void UpdateUI(AccelerometerReading accelerometerReading)
        {
            Vector3 acceleration = accelerometerReading.Acceleration; 

            // 输出 X Y Z 的值
            lblMsg.Text = "acceleration.X: " + acceleration.X.ToString("0.0");
            lblMsg.Text += Environment.NewLine;
            lblMsg.Text += "acceleration.Y: " + acceleration.Y.ToString("0.0");
            lblMsg.Text += Environment.NewLine;
            lblMsg.Text += "acceleration.Z: " + acceleration.Z.ToString("0.0");
        }
    }
}

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索传感器
, 三轴加速度传感器
, 加速度传感器
, using
, system
, 方向
, 加速度
, 方向传感器
, 传感
android测力计
windows phone、windowsphone、windows phone 10、windowsphone手机、windowsphone应用商店,以便于您获取更多的相关知识。

时间: 2024-08-31 06:02:07

与众不同windows phone (18)的相关文章

与众不同 windows phone (18) - Device(设备)之加速度传感器, 数字罗盘传感器

原文:与众不同 windows phone (18) - Device(设备)之加速度传感器, 数字罗盘传感器 [索引页][源码下载] 与众不同 windows phone (18) - Device(设备)之加速度传感器, 数字罗盘传感器 作者:webabcd 介绍与众不同 windows phone 7.5 (sdk 7.1) 之设备 加速度传感器(加速度计) 数字罗盘(磁力计) 示例1.演示如何使用加速度传感器AccelerometerDemo.xaml <phone:PhoneAppli

与众不同windows phone (6) Isolated Storage(独立存储)

介绍 与众不同 windows phone 7.5 (sdk 7.1) 之独立存储 概述 独立存储的读/写的Demo 读/写 key/value 形式数据到独立存储的快捷方法 示例 1.概述 Summary.xaml <phone:PhoneApplicationPage x:Class="Demo.IsolatedStorageDemo.Summary" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/prese

与众不同windows phone (47)

8.0 其它: 锁屏信息和锁屏背景, 电池状态, 多分辨率, 商店, 内置协议, 快速恢复 介绍 与众不同 windows phone 8.0 之 其它 锁屏信息和锁屏背景 电池状态 多分辨率应用 与 Windows Phone 商店相关的操作 系统的内置协议 快速恢复应用 App.xaml.cs 的说明 manifest 的说明 示例 1.演示如何发送信息到锁屏,以及如何修改锁屏背景 Others/LockScreen.xaml <phone:PhoneApplicationPage x:Cl

与众不同windows phone (46) 8.0 通信: Socket, 其它

介绍 与众不同 windows phone 8.0 之 通信 Socket Demo 获取当前连接的信息 http rss odata socket bluetooth nfc voip winsock 示例 1.演示 socket tcp 的应用 (本例既做服务端又做客户端) Communication/SocketDemo.xaml <phone:PhoneApplicationPage x:Class="Demo.Communication.SocketDemo" xmln

与众不同windows phone (45) 8.0 语音: TTS, 语音识别, 语音命令

介绍 与众不同 windows phone 8.0 之 语音 TTS(Text To Speech) 语音识别 语音命令 示例 1.演示 TTS(Text To Speech)的应用 Speech/TTS.xaml <phone:PhoneApplicationPage x:Class="Demo.Speech.TTS" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmln

与众不同windows phone (44) 8.0 位置和地图

介绍 与众不同 windows phone 8.0 之 位置和地图 位置(GPS) - Location API 诺基亚地图 示例 1.演示新 Location API 的应用 GPS/Demo.xaml <phone:PhoneApplicationPage x:Class="Demo.GPS.Demo" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x=&qu

与众不同windows phone (43)

8.0 相机和照片: 镜头的可扩展性, 图片的可扩展性, 图片的自动上传扩展 介绍 与众不同 windows phone 8.0 之 相机和照片 镜头的可扩展性 图片的可扩展性 图片的自动上传扩展 示例 1.演示如何将本 app 注册为镜头扩展 CameraAndPhoto/LensExtensibility.xaml <phone:PhoneApplicationPage x:Class="Demo.CameraAndPhoto.LensExtensibility" xmlns

与众不同windows phone (42)

8.0 相机和照片: 通过 PhotoCaptureDevice 捕获照片 介绍 与众不同 windows phone 8.0 之 相机和照片 通过 PhotoCaptureDevice 捕获照片 示例 演示 PhotoCaptureDevice(wp8)的应用 CameraAndPhoto/PhotoCaptureDeviceDemo.xaml <phone:PhoneApplicationPage x:Class="Demo.CameraAndPhoto.PhotoCaptureDev

与众不同windows phone (41)

8.0 相机和照片: 通过 AudioVideoCaptureDevice 捕获视频和音频 介绍 与众不同 windows phone 8.0 之 相机和照片 通过 AudioVideoCaptureDevice 捕获视频和音频 示例 演示 AudioVideoCaptureDevice(wp8)的应用 CameraAndPhoto/AudioVideoCaptureDeviceDemo.xaml <phone:PhoneApplicationPage x:Class="Demo.Came