.NET 4.0 Location : 查看传感器状态变化

.NET 4.0 中关于访问传感器状态进行了封装。在.NET4.0 beta2中我们可以用GeoLocationProvider。但是现在我们可以方便的使用GeoCoordinateWatcher类进行获知。较以往的GeoLocationProvider现在的GeoCoordinateWatcher结构如下:

image

具体代码如下:

using System;
using System.Device.Location;
namespace LocationStatusChange
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Outputting location updates, press any key to exit...");
            LocationWatcher watcher = new LocationWatcher();
            Console.ReadKey();
        } 

    }
    class LocationWatcher
    {
        private GeoCoordinateWatcher provider; 

        public LocationWatcher()
        {
            this.provider = new GeoCoordinateWatcher();
            this.provider.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(provider_StatusChanged);
            this.provider.Start();
        } 

        void provider_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
        {
            Console.WriteLine("LocationStatus: " + e.Status.ToString());
        }
    }
} 

这个是运行的效果:

image

当我修改传感器让其不可用时:

image

这时应用程序能正确的捕捉到状态:

image

 

呵呵.NET4.0 真的很强大。

posted @ 2010-07-08 11:56  yangjingbo  阅读(376)  评论(0)    收藏  举报