使用重力感应器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">
-
-
-
<Grid x:Name="LayoutRoot" Background="Transparent">
-
<Grid.RowDefinitions>
-
<RowDefinition Height="Auto"/>
-
<RowDefinition Height="*"/>
-
</Grid.RowDefinitions>
-
-
-
<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>
-
-
-
<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;
- }
- }
- }
本文转自linzheng 51CTO博客,原文链接:http://blog.51cto.com/linzheng/1079232