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

简介:

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

 


 
 
  1. 代码  
  2.  
  3.  
  4. using System;  
  5. using System.Security;  
  6.  
  7. namespace Microsoft.Devices.Sensors  
  8. {  
  9.     // Summary:  
  10.     //     Provides Windows?Phone applications access to the device’s accelerometer  
  11.     //     sensor.  
  12.     public sealed class Accelerometer : IDisposable  
  13.     {  
  14.         // Summary:  
  15.         //     Creates a new instance of the Microsoft.Devices.Sensors.Accelerometer object.  
  16.         [SecuritySafeCritical]  
  17.         public Accelerometer();  
  18.  
  19.         // Summary:  
  20.         //     Gets the current state of the accelerometer. The value is a member of the  
  21.         //     Microsoft.Devices.Sensors.SensorState enumeration.  
  22.         //  
  23.         // Returns:  
  24.         //     Type Microsoft.Devices.Sensors.SensorState.  
  25.         public SensorState State { get; }  
  26.  
  27.         // Summary:  
  28.         //     Occurs when new data arrives from the accelerometer.  
  29.         public event EventHandler<AccelerometerReadingEventArgs> ReadingChanged;  
  30.  
  31.         // Summary:  
  32.         //     Releases the managed and unmanaged resources used by the Microsoft.Devices.Sensors.Accelerometer.  
  33.         [SecuritySafeCritical]  
  34.         public void Dispose();  
  35.         //  
  36.         // Summary:  
  37.         //     Starts data acquisition from the accelerometer.  
  38.         [SecuritySafeCritical]  
  39.         public void Start();  
  40.         //  
  41.         // Summary:  
  42.         //     Stops data acquisition from the accelerometer.  
  43.         [SecuritySafeCritical]  
  44.         public void Stop();  
  45.     }  

 

 

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

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

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

实例

 


 
 
  1. 代码  
  2.  
  3.  
  4. MainPage.xaml  
  5. <phone:PhoneApplicationPage   
  6.     x:Class="SilverlightAccelerometer.MainPage" 
  7.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  8.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
  9.     xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" 
  10.     xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" 
  11.     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  12.     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  13.     FontFamily="{StaticResource PhoneFontFamilyNormal}" 
  14.     FontSize="{StaticResource PhoneFontSizeNormal}" 
  15.     Foreground="{StaticResource PhoneForegroundBrush}" 
  16.     SupportedOrientations="PortraitOrLandscape" Orientation="Portrait" 
  17.     mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768" 
  18.     shell:SystemTray.IsVisible="True"> 
  19.  
  20.     <!--LayoutRoot contains the root grid where all other page content is placed--> 
  21.     <Grid x:Name="LayoutRoot" Background="Transparent"> 
  22.         <Grid.RowDefinitions> 
  23.             <RowDefinition Height="Auto"/> 
  24.             <RowDefinition Height="*"/> 
  25.         </Grid.RowDefinitions> 
  26.  
  27.         <!--TitlePanel contains the name of the application and page title--> 
  28.         <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28"> 
  29.             <TextBlock x:Name="ApplicationTitle" Text="SILVERLIGHT ACCELEROMETER" Style="{StaticResource PhoneTextNormalStyle}"/> 
  30.             <TextBlock x:Name="PageTitle" Text="main page" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/> 
  31.         </StackPanel> 
  32.  
  33.         <!--ContentPanel - place additional content here--> 
  34.         <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> 
  35.             <TextBlock Name="txtblk" 
  36.                         HorizontalAlignment="Center" 
  37.                         VerticalAlignment="Center" /> 
  38.         </Grid> 
  39.     </Grid> 
  40. </phone:PhoneApplicationPage> 

 


 
 
  1. 代码  
  2.  
  3.  
  4. MainPage.xaml.cs  
  5. using System;  
  6. using System.Windows;  
  7. using System.Windows.Controls;  
  8. using Microsoft.Devices.Sensors;  
  9. using Microsoft.Phone.Controls;  
  10.  
  11. namespace SilverlightAccelerometer  
  12. {  
  13.     public partial class MainPage : PhoneApplicationPage  
  14.     {  
  15.         public MainPage()  
  16.         {  
  17.             InitializeComponent();  
  18.  
  19.             Accelerometer acc = new Accelerometer();//初始化一个重力感应器的类  
  20.             acc.ReadingChanged += OnAccelerometerReadingChanged;//触发重力感应的事件  
  21.  
  22.             try  
  23.             {  
  24.                 acc.Start();//开始加速计重力感应  
  25.             }  
  26.             catch (Exception exc)  
  27.             {  
  28.                 txtblk.Text = exc.Message;  
  29.             }  
  30.         }  
  31.  
  32.         void OnAccelerometerReadingChanged(object sender, AccelerometerReadingEventArgs args)  
  33.         {  
  34.             string str = String.Format("X = {0:F2}\n" +     //x轴表示屏幕的左右    
  35.                                        "Y = {1:F2}\n" +      //y轴表示屏幕的上下   
  36.                                        "Z = {2:F2}\n\n" +    //z轴表示屏幕正上方的上下   
  37.                                        "Magnitude = {3:F2}\n\n" +  
  38.                                        "{4}",   
  39.                                        args.X, args.Y, args.Z,   
  40.                                        Math.Sqrt(args.X * args.X + args.Y * args.Y +   //计算加速度  
  41.                                                                    args.Z * args.Z),                                         
  42.                                        args.Timestamp);  
  43.  
  44.             if (txtblk.CheckAccess())//判断线程是否允许访问  
  45.             {  
  46.                 SetTextBlockText(txtblk, str);  
  47.             }  
  48.             else  
  49.             {  
  50.                 //重新激活线程  
  51.                 txtblk.Dispatcher.BeginInvoke(new SetTextBlockTextDelegate(SetTextBlockText),   
  52.                                               txtblk, str);  
  53.             }  
  54.         }  
  55.  
  56.         delegate void SetTextBlockTextDelegate(TextBlock txtblk, string text);  
  57.  
  58.         void SetTextBlockText(TextBlock txtblk, string text)  
  59.         {  
  60.             txtblk.Text = text;  
  61.         }  
  62.     }  

 

 

 

 


本文转自linzheng 51CTO博客,原文链接:http://blog.51cto.com/linzheng/1079232


相关文章
|
消息中间件 C++ Windows
02 MFC - Windows 编程模型
02 MFC - Windows 编程模型
126 0
|
12月前
|
网络协议 API Windows
MASM32编程调用 API函数RtlIpv6AddressToString,windows 10 容易,Windows 7 折腾
MASM32编程调用 API函数RtlIpv6AddressToString,windows 10 容易,Windows 7 折腾
|
12月前
|
Windows
[原创]用MASM32编程获取windows类型
[原创]用MASM32编程获取windows类型
|
12月前
|
JavaScript 前端开发 API
MASM32编程通过WMI获取Windows计划任务
MASM32编程通过WMI获取Windows计划任务
|
12月前
|
API Windows
MASM32编程获取Windows当前桌面主题名
MASM32编程获取Windows当前桌面主题名
|
编译器 开发工具 C语言
解锁QtCreator跨界神技!Windows下轻松驾驭OpenCV动态库,让你的跨平台开发如虎添翼,秒变视觉编程大师!
【8月更文挑战第4天】QtCreator是一款强大的跨平台IDE,便于创建多平台应用。本教程教你如何在Windows环境下集成OpenCV库至Qt项目。首先,下载匹配MinGW的OpenCV预编译版并解压。接着,在QtCreator中新建或打开项目,并在.pro文件中添加OpenCV的头文件和库文件路径。确保编译器设置正确。随后编写测试代码,例如加载和显示图片,并进行编译运行。完成这些步骤后,你就能在QtCreator中利用OpenCV进行图像处理开发了。
614 6
|
C++ Windows
[笔记]Windows核心编程《番外篇》几种常见的执行命令行方法
[笔记]Windows核心编程《番外篇》几种常见的执行命令行方法
217 0
|
数据库 Windows
超详细步骤解析:从零开始,手把手教你使用 Visual Studio 打造你的第一个 Windows Forms 应用程序,菜鸟也能轻松上手的编程入门指南来了!
【8月更文挑战第31天】创建你的第一个Windows Forms (WinForms) 应用程序是一个激动人心的过程,尤其适合编程新手。本指南将带你逐步完成一个简单WinForms 应用的开发。首先,在Visual Studio 中创建一个“Windows Forms App (.NET)”项目,命名为“我的第一个WinForms 应用”。接着,在空白窗体中添加一个按钮和一个标签控件,并设置按钮文本为“点击我”。然后,为按钮添加点击事件处理程序`button1_Click`,实现点击按钮后更新标签文本为“你好,你刚刚点击了按钮!”。
1217 0
|
Java C++
jni编程(windows+JDK11+clion)
jni编程(windows+JDK11+clion)
220 1
|
API C++ Windows
windows编程入门_链接错误的配置
windows编程入门_链接错误的配置
122 0