zoukankan      html  css  js  c++  java
  • Windows Phone 7(accelerometer)重力感应编程

    使用重力感应器accelerometer,需要引用类库Microsoft.Devices.Sensors ,所以需要在WMAppManifest.xml
    加上
     <Capability Name="ID_CAP_SENSORS" />

    代码

    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.
    [SecuritySafeCritical]
    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<AccelerometerReadingEventArgs> ReadingChanged;

    // Summary:
    // Releases the managed and unmanaged resources used by the Microsoft.Devices.Sensors.Accelerometer.
    [SecuritySafeCritical]
    public void Dispose();
    //
    // Summary:
    // Starts data acquisition from the accelerometer.
    [SecuritySafeCritical]
    public void Start();
    //
    // Summary:
    // Stops data acquisition from the accelerometer.
    [SecuritySafeCritical]
    public void Stop();
    }
    }

    X轴表示左右方向的重力大小 

    Y轴表示上下方向的重力大小 

    Z轴表示屏幕正上方下面的的重力大小 

    实例

    代码

    MainPage.xaml
    <phone:PhoneApplicationPage
    x:Class="SilverlightAccelerometer.MainPage"
    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
    ="PortraitOrLandscape" Orientation="Portrait"
    mc:Ignorable
    ="d" d:DesignWidth="480" d:DesignHeight="768"
    shell:SystemTray.IsVisible
    ="True">

    <!--LayoutRoot contains the root grid where all other page content is placed-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
    <Grid.RowDefinitions>
    <RowDefinition Height="Auto"/>
    <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <!--TitlePanel contains the name of the application and page title-->
    <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
    <TextBlock x:Name="ApplicationTitle" Text="SILVERLIGHT ACCELEROMETER" Style="{StaticResource PhoneTextNormalStyle}"/>
    <TextBlock x:Name="PageTitle" Text="main page" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
    </StackPanel>

    <!--ContentPanel - place additional content here-->
    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
    <TextBlock Name="txtblk"
    HorizontalAlignment
    ="Center"
    VerticalAlignment
    ="Center" />
    </Grid>
    </Grid>
    </phone:PhoneApplicationPage>
    代码

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

  • 相关阅读:
    Java命名规范
    用U盘安装系统2
    将Sublime Text3添加到右键菜单中
    Git版本控制使用介绍
    Sublime Text各种插件使用方法
    安装Sublime Text 3插件
    Sublime Text修改显示图标
    卸载Visual Studio Code后删除右键Open with Code…
    做最好的自己(Be Your Personal Best)
    Eclipse全屏及插件下载
  • 原文地址:https://www.cnblogs.com/linzheng/p/1948801.html
Copyright © 2011-2022 走看看