在当今的APP时代,重力感应是引领各种创意应用的重要方式。
2:在 LoginPop 得到异步调用WCF的验证结果后,对结果进行处理:
尚未开始前:
2:增加 开始 按钮的Click事件
由于WP7重感数据类,采用后台生成线程,50毫秒监测一次,因此在处理事件中直接改变界面UI会报错,只能采取委托的办法讲数据写到UI中!
至此,重力感应数据呈现就OK了,运行后得到文章最前的画面,此数据可运用到各种游戏、地图、行业应用中!
在学习过程中,小生本来是想在界面上再增加一个【关闭重力感应】的功能,原本觉得没多大问题,但在查找资料中才发现,被微软当宝推出的 WP7在最新版本 7.5 中,也没有内置 关闭重感 的API,资料中表示:如想关闭重感,只能修改注册表相关项!而网上可见的相关应用,无一不是修改注册表而成。
可以说,没有重力感应的突破,智能时代不会这么快速来临。
那么,这次就开始获取机器重力感应的数据,作为继续深入WP7的突破口吧!
打开上次完成的App访问WCF项目,在项目基础上增加重力感应的应用。
流程:WCF身份认证正确后,系统给出统计数据页面,点击开始后,开始监测重力感应数据;点击停止,则停止监测。
程序完成后应得到如下应用:

首先,要导航到重感页面,之前的LoginPop登录页面要做些改动:
1:改动 LoginPho 的构造函数,将主页面传递过去,让 LoginPop 在得到验证结果后,对主页面 MainPage 做出处理:
MainPage main = null;
public LoginPop(MainPage main)
{
InitializeComponent();
this.main = main;
}
2:在 LoginPop 得到异步调用WCF的验证结果后,对结果进行处理:
switch(e.Result.ToString()){
case "0":
lbl_error.Text = "验证正确";
DialogResult = true;
break;
case "1":
lbl_error.Text = "用户名错误";
break;
case "2":
lbl_error.Text = "密码错误";
break;
default:
lbl_error.Text = "未知错误!";
break;
}
//如果验证正确,关闭登录框,导航到重感页面
if (DialogResult == true)
{
this.Close();
main.NavigationService.Navigate(new Uri("/Gravity/GravityGame.xaml", UriKind.Relative));
}
3:增加重感页面
在项目下增加 Gravity 文件夹,加入 GravityGame.xaml 页面,此页面有【开始】按钮,点击后,将会完成记录重感数据的工作。
<phone:PhoneApplicationPage
x:Class="PhoneApp1.Gravity.GravityGame"
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="Landscape" Orientation="Landscape"
mc:Ignorable="d" d:DesignHeight="480" d:DesignWidth="728"
shell:SystemTray.IsVisible="True">
<!--LayoutRoot 是包含所有页面内容的根网格-->
<Grid x:Name="LayoutRoot" Background="White">
<Button Content="开 始" Height="53" HorizontalAlignment="Left" Margin="12,12,0,0" Name="btn_start" VerticalAlignment="Top" Width="115" Style="{StaticResource BtnStyleDefault}" BorderThickness="0" Padding="0" Click="btn_start_Click" Cursor="Eraser" Background="Transparent" />
<TextBlock Height="30" HorizontalAlignment="Left" Margin="164,132,0,0" Name="textBlock1" Text="左:" VerticalAlignment="Top" Foreground="Black" Width="43" />
<TextBlock Foreground="Black" Height="30" HorizontalAlignment="Left" Margin="228,132,0,0" Name="lbl_left" Text="左" VerticalAlignment="Top" Width="135" />
<TextBlock Foreground="Black" Height="30" HorizontalAlignment="Left" Margin="164,184,0,0" Name="textBlock2" Text="右:" VerticalAlignment="Top" Width="43" />
<TextBlock Foreground="Black" Height="30" HorizontalAlignment="Left" Margin="228,184,0,0" Name="lbl_right" Text="右" VerticalAlignment="Top" Width="135" />
<TextBlock Foreground="Black" Height="30" HorizontalAlignment="Left" Margin="164,240,0,0" Name="textBlock3" Text="上:" VerticalAlignment="Top" Width="43" />
<TextBlock Foreground="Black" Height="30" HorizontalAlignment="Left" Margin="228,240,0,0" Name="lbl_top" Text="上" VerticalAlignment="Top" Width="135" />
<TextBlock Foreground="Black" Height="30" HorizontalAlignment="Left" Margin="164,295,0,0" Name="textBlock5" Text="下:" VerticalAlignment="Top" Width="43" />
<TextBlock Foreground="Black" Height="30" HorizontalAlignment="Left" Margin="228,295,0,0" Name="lbl_bottom" Text="下" VerticalAlignment="Top" Width="135" />
<TextBlock Foreground="Black" Height="30" HorizontalAlignment="Left" Margin="422,132,0,0" Name="textBlock7" Text="前:" VerticalAlignment="Top" Width="43" />
<TextBlock Foreground="Black" Height="30" HorizontalAlignment="Right" Margin="0,132,107,0" Name="lbl_before" Text="前" VerticalAlignment="Top" Width="135" />
<TextBlock Foreground="Black" Height="30" HorizontalAlignment="Left" Margin="422,184,0,0" Name="textBlock9" Text="后:" VerticalAlignment="Top" Width="43" />
<TextBlock Foreground="Black" Height="30" HorizontalAlignment="Left" Margin="486,184,0,0" Name="lbl_after" Text="后" VerticalAlignment="Top" Width="135" />
</Grid>
</phone:PhoneApplicationPage>
尚未开始前:

重感页面首先需要引用重感监测类:Microsoft.Devices.Sensors 与 XNA运行框架: Microsoft.Xna.Framework
这样在机器移动时,重感监测类可以监测移动,并利用XNA框架得到具体移动数据。
1:申明类变量:
//重力感应对象
Accelerometer acc;
//标识系统状态
int state = 0;
2:增加 开始 按钮的Click事件
private void btn_start_Click(object sender, RoutedEventArgs e)
{
//首先监测是否支持重力感应
if (!Accelerometer.IsSupported)
{
MessageBox.Show("此设备不支持重力感应");
return;
}
if (state == 0)
{
//开始监测
btn_start.Content = "停 止";
btn_start.SetValue(Button.StyleProperty, Application.Current.Resources["BtnStyleMove"]);
state = 1;
//如支持 开始初始化重力感应对象
acc = new Accelerometer();
//为对象增加监测重力改变事件
acc.CurrentValueChanged += new EventHandler<SensorReadingEventArgs<AccelerometerReading>>(Acc_CurrentValueChanged);
//开始监测
acc.Start();
}
else
{
//停止监测
btn_start.Content = "开 始";
btn_start.SetValue(Button.StyleProperty, Application.Current.Resources["BtnStyleDefault"]);
state = 0;
//删除监测重力改变事件
acc.CurrentValueChanged -= new EventHandler<SensorReadingEventArgs<AccelerometerReading>>(Acc_CurrentValueChanged);
//停止监测
acc.Stop();
}
}
3:增加重力改变处理事件
//重力感应改变事件
private void Acc_CurrentValueChanged(object sender, SensorReadingEventArgs<AccelerometerReading> e)
{
//将监测到的数据改变用委托给写入界面的方法
Deployment.Current.Dispatcher.BeginInvoke(
() => Acc_WhiteUi(e)
);
}
由于WP7重感数据类,采用后台生成线程,50毫秒监测一次,因此在处理事件中直接改变界面UI会报错,只能采取委托的办法讲数据写到UI中!
4:增加数据UI界面的委托实现:
//将数据写入界面UI的委托方法
private void Acc_WhiteUi(SensorReadingEventArgs<AccelerometerReading> e)
{
//右
lbl_right.Text = Convert.ToString(e.SensorReading.Acceleration.X);
//左
lbl_left.Text = Convert.ToString(1 - e.SensorReading.Acceleration.X);
//下
lbl_bottom.Text = Convert.ToString(e.SensorReading.Acceleration.Y);
//上
lbl_top.Text = Convert.ToString(1 - e.SensorReading.Acceleration.Y);
//后
lbl_after.Text = Convert.ToString(e.SensorReading.Acceleration.Z);
//前
lbl_before.Text = Convert.ToString(1 - e.SensorReading.Acceleration.Z);
}
至此,重力感应数据呈现就OK了,运行后得到文章最前的画面,此数据可运用到各种游戏、地图、行业应用中!
在学习过程中,小生本来是想在界面上再增加一个【关闭重力感应】的功能,原本觉得没多大问题,但在查找资料中才发现,被微软当宝推出的 WP7在最新版本 7.5 中,也没有内置 关闭重感 的API,资料中表示:如想关闭重感,只能修改注册表相关项!而网上可见的相关应用,无一不是修改注册表而成。
微软对芒果如此重视,却将开发者束缚得憋手蹩脚,有些想问,微软你还想不想混App了?
此小
Demo 下载地址(含WCF程序):
http://download.youkuaiyun.com/detail/begin_game/4364507