zoukankan      html  css  js  c++  java
  • 一起学Windows Phone7开发(十五. Device)

    Accelerometer

    Accelerometer API用来获取重力加速传感器的数据,从而用来开发游戏等程序。

    if (am != null)

        am.Stop();

    am = new Accelerometer();

    am.ReadingChanged += new EventHandler<AccelerometerReadingEventArgs>(am_ReadingChanged);//监控重力加速数据。

    try//因为启动时,如果出错会抛出异常,所以要用try块来处理。

    {

         am.Start();//开始获取数据

    }

    catch(AccelerometerFailedException e)

    {

    }

     am.Stop();//停止获取数据。

    void am_ReadingChanged(object sender, AccelerometerReadingEventArgs e)

    {

         //获取数。

    }

     

    Location Service

    定位API,提供了GPSWIFILBS等多种方式获取定位数据的方式。并且可以同时使用一种或多种方种方式来获取定位数据。

        watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.Default);

        watcher.MovementThreshold = 35;// 相对于最后一个 PositionChanged 事件中的坐标必须移动的距离(以米为单位),移动该距离之后位置提供程序将引发另一个 PositionChanged 事件。

        watcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(watcher_PositionChanged);//监控定位数据变化

    watcher.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(watcher_StatusChanged);//监控服务状态变化

     

    watcher.Start();//启动定位服务来获取数据。但这时不一定服务可用,或者等待启动时间过久,这时就可以调用带超时的启动方法,如果超时,就停止启动。

    watcher.TryStart(true, TimeSpan.FromMilliseconds(5000));

     if (watcher.Status == GeoPositionStatus.Ready)

           this.PageTitle.Text = "service start";

     else

           this.PageTitle.Text = "service not start";

    watcher.Stop();//停止服务。

     

      void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)

      {

    //可以得到的定位数据

    e.Position.Location.Altitude.ToString("0.000")

    e.Position.Location.Latitude.ToString("0.000")

            e.Position.Location.Longitude.ToString("0.000")

            e.Position.Location.Speed.ToString("0.000");

       }

    void watcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)

    {

           e.Status.ToString();//获取服务状态

    }

     

     

    因为是在模拟器上,以上这两个接口其实都是不可用的,但是如果要开发程序过程中,一定要用这些数据又怎么办呢?其实WP7上又提供了另外的方法可以虚拟这些设备来接收模拟数据。这样就可以在开发游戏的时候用这样的方法来测试自已的游戏了。

     

     

     

    FM Radio

    WP7中提供了收音机的API,由此可以看出来,未来真机里的是有收音机的。这个收音机的APIsingleton模式的,也就是说一个应用只能有一个收音机实例。不过目能够收听到的区域只有三个:EuropeJapanUnited States

     

    FMRadio radio = FMRadio.Instance;

    radio.CurrentRegion = RadioRegion.Europe;

    radio.Frequency = 100.5;

    radio.PowerMode = RadioPowerMode.On;

     

    Vibrate Controller

    振动控制器用来启动和停止WP7上的振动器。

    VibrateController vc = VibrateController.Default;

    vc.Start(TimeSpan.FromMilliseconds(100));

    vc.Stop();

     

     

    WP7有提供的设备接口看来还是挺多的,但是却一直没发现蓝牙、WIFI等的接口,不知道是WP7上没有这些设备,或者是不提供这些接口?还是因为是beta版的sdk没有加进来呢?

     

     

  • 相关阅读:
    Springboot如何优雅的解决ajax+自定义headers的跨域请求
    提升开发效率的一款mybatis开发神器
    深究Spring中Bean的生命周期
    阿里Canal框架(数据同步中间件)初步实践
    从技术角度分析推荐系统案例
    记一次token安全认证的实践
    为什么Redis 单线程却能支撑高并发?
    Python 派生类子类继承类
    Python 定制类与其对象的创建和应用
    Python 字典的创建赋值和动态扩展
  • 原文地址:https://www.cnblogs.com/randylee/p/1812376.html
Copyright © 2011-2022 走看看