WP7--13
(注:本节所学的在模拟器上不能测试,不过看看不错,至少了解一下!重力加速器,对做游戏比较有帮助!)
设备开发
1.获取设备信息;
2.Accelerometer(重力加速传感器);
3.Location Service(位置服务);
4.FM Radio(收音机);
5.Vibrate Controller(振动控制器);
6.Reactive Extension(反应性框架)
1.获取设备信息:
(1).命名空间:Microsoft.Phone.Info
a.UserExtendedProperties:只能获取一个信息ANID (anonymous identifier)
b.DeviceExtendedProperties
(2).UnAuthorizedAccessException
ID_CAP_IDENTITY_DEVICE in WMAppMainifest.xml
(3).示例:
string Manufacturer=DeviceExtendedProperties.GetValue ("DeviceManufacturer").ToString();
string anid=UserExtendedProperties.GetValues ("ANID").ToString();
代码:
页面Load事件:
this.PageTitle.Text=DeviceExtendedProperties.GetValue ("DeviceName").ToString();
2.Accelerometer
(1).获取重力加速传感器的数据:用于游戏的开发,手机小工具水平仪的开发
(2).命名空间:Microsoft.Devices.Sensors
(3).示例
Accelerometer am=new Accelerometer();
am.ReadingChange+=new EventHandle<AccelerometerReadingEventArgs>(am_ReadingChanged);//监控重力加速数据
//因为启动时,如果出错会抛出异常,所以要用try块来处理
try
{am.Start();//开始获取数据}
catch(AccelerometerFailedException e)
{ }
am.Stop();//停止获取数据
void am_ReadingChanged(object sender,AccelerometerReadingEventArgs e)
{//获取数据 }
代码:在页面Load事件:
Accelerometer am=new Accelerometer();
am.ReadingChanged+==new EventHandle<AccelerometerReadingEventArgs>(am_ReadingChanged);
am.Start();
在ReadingChanged事件中:(跨线程访问)
System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() e>
{
textBlock1.Text="X:"+e.X.ToString();
textBlock2.Text="Y:"+e.Y.ToString();
textBlock3.Text="Z:"+e.Z.ToString();
})
3.Location Service
(1).获取定位数据:经度、纬度的信息
(2).命名空间:System.Device.Location
(3).示例
watcher=new GeoCooedinateWatcher(GeoPositionAccuracy.Default);
//相对于最后一个PositionChanged事件中的坐标鼻息移动的距离
//设置不可太大,太大精度可能不好;设置太小,可能侦测不到
watcher.MovementThreshold=35;//是以米为单位的
watcher.PositionChanged+=new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(watcher_PositionChanged);
watcher.StatusChanged+=new EventHandler<GeoPositionStatusChangedEventArgs>(watcher_StatusChanged);
watcher.TryStart(true,TimeSpan.FromMilliseconds(5000));
void watcher_PositionChanged(object sender,GeoPositionChangedEventArgs<GeoCoordinate>e)
{//可以得到的定位数据}
void watcher_StatusChanged(object sender,GeoPositionStatusChangedEventArgs e)
{//启动服务状态}
代码:在页面Load事件:
GeoCoordinateWatcher watch=new GeoCoordinateWatcher();
watch.watcher.MovementThreshold=35;
watch.PositionChanged+=new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(watch_PositionChanged);
watch.StatusChanged+=new EventHandler<GeoPositionStatusChangedEventArgs>(watch_StatusChanged);
watch.Start();
在watch.StatusChanged事件中:
this.PageTitle.Text=e.Status.ToString();
4.FM Radio
(1).一个应用只能有一个收音机
(2).能够收听道德区域:Europe\Japan\United States
(3).命名空间:Microsoft.Devices.Radio
(4).示例
FMRadio radio=FMRadio.Inatance;//单键模式
radio.CurrentRegion=RadioRegion.Europe;//收听区域
radio.Frequency=100.5;//频段
radio.PowerMode=RadioPowerMode.On;//打开或关闭
5.Vibrate Controller
(1).启动或停止振动器
(2).命名空间:Microsoft.Devices
VibrateController vc=VibrateController.Default;
vc.Start(TimeSpan.FromMilliseconds(100));
vc.Stop();
6.Reactive Extension
(1).基本信息:
为指定异步操作指定回调;事件处理程序的时候使用反应性编程;
在异步操作完成或者事件触发的时候,就会调用方法并作为对该事件的反应;在这个模型中,数据流、异步请求、事件都可以作为可观察序列,并且接受异步信息;主要用在:filtering events\composing multiple asynchronous web service requests\emulating data streams(模拟数据流)
(2).模拟数据:模拟Accelerometer数据
示例
Thread thread=new Thread(StartAccelerometerEmulation);
am=new Accelerometer();
vararo=Observable.FromEvent<AccelerometerReadingEventArgs>(eh=>am.ReadingChanged+=eh,eh=>am.ReadingChanged-=eh);
am.Start();
static IEnumerable<Vector3>EmulateAccelerometerReading()
{//生成模拟数据}
private void StartAccelerometerEmulation()
{//开始模拟数据}
void InvokeAccelerometerReadingChanged(Vector3 data)
{
Deployment.Current.Dispatcher.BeginInvoke[()=>AccelerometerReadingChanged(data)];
}
void AccelerometerReadingChanged(Vector3 data)
{//获取模拟数据}
代码:
using System.Threading;
using Microsoft.Xna.Framwork;
using Microsoft.Phone.Reactive;
(3).模拟Location数据
Thread t=new Thread(StartLocationEmulation);
watcher=new GeoCoordinateWatcher(GeoPositionAccuracy.Default);
var gcwo=
Observable.FromEvent<GeoPositionChangedEventArgs<GeoCoordinate>>(eh=>watcher.PositionChanged+=eh,eh=>watcher.PositionChanged-=eh);
watcher.Start();
private void StartLocationEmulation()
{//开始模拟数据}
static IEnumerable
<GeoPositionChangedEventArgs<GeoCoordinate>>EmulatePositionChangedEvents()
{//生成模拟数据}
private void MyPositionChanged(GeoPositionChangedEventArgs<GeoCoordinate>e)
{//获取模拟数据}
代码:
在Load事件中:
Thread t=new Thread(StartLocationEmulation);
t.Start();
GeoCoordinateWatcher watcher=new GeoCoordinateWatcher();
Observable.FromEvent<GeoPositionChangedEventArgs<GeoCoordinate>>(eh=>watcher.PositionChanged+=eh,eh=>watcher.PositionChanged-=eh);
watcher.Start();
static IEnumerable
<GeoPositionChangedEventArgs<GeoCoordinate>>EmulatePositionChangedEvents()
{
Random random=new Random();
while(true)
{//[-90,90]
double latitude=(random.NextDouble()*180.00)-90.0;
//[-180,180]
double longitude=(random.NextDouble()*360.0)-180.0;
yield return new GeoPositionChangedEventArgs<GeoCoordiante>(new GeoPosition<GeoCoordinate>(DateTimeOffset.Now,new GeoCoordinate(latitude,longitude)));
Thread.Sleep(random.Next(2000));
}
}
private void StartLocationEmulation()
{
var position=EnulatePositionChangedEvents().ToObservabl();
position.Subscribe(ev=>wait_PositionChanged(null,ev));
}
private void wait_PositionChanged(object sender,GeoPositionChangedEventArgs<GeoCoordinate>e)
{
Depleyment.Current.Dispatchar.BeginInvoke(()=>MyPositionChanged(e));
}
private void MyPositionChanged(GeoPositionChangedEventArgs<GeoCoordinate>e)
{
this.textBlock1.Text="Time:"+e.Position.Timesage.ToString("yyyy-MM-dd hh/mm/ss");
this.textBlock2.Text="Latitude:"+e.Position.Loation.Latitude.ToString("0.00");
this.textBlock3.Text="Longitude:"+e.Position.Location.Lengitude.ToString("0.00");
}