2012-10-29 71 views
4

我是C#的初学者,但我已经使用了很多Java。我正尝试在我的应用程序中使用以下代码来获取位置数据。我想提出一个Windows 8桌面应用程序来使用我的设备的GPS传感器:在Windows 8桌面应用中获取位置

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using Windows.Devices.Sensors; 
using Windows.Devices.Geolocation; 
using Windows.Devices.Geolocation.Geoposition; 
using Windows.Foundation; 

namespace Hello_Location 
{ 
    public partial class Form1 : 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     async private void Form1_Load(object sender, EventArgs e) 
     { 
      Geolocator loc = new Geolocator(); 
      try 
      { 
       loc.DesiredAccuracy = PositionAccuracy.High; 
       Geoposition pos = await loc.GetGeopositionAsync(); 
       var lat = pos.Coordinate.Latitude; 
       var lang = pos.Coordinate.Longitude; 
       Console.WriteLine(lat+ " " +lang); 
      } 
      catch (System.UnauthorizedAccessException) 
      { 
       // handle error 
      } 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 

     } 
    } 
} 

我得到这个错误:

'await' requires that the type 'Windows.Foundation.IAsyncOperation' have a suitable GetAwaiter method. Are you missing a using directive for 'System'? C:\Users\clidy\documents\visual studio 2012\Projects\Hello-Location\Hello-Location\Form1.cs

我该如何解决这个问题?

如果您可以指向我一些C#位置资源和Windows desktop应用程序的传感器API,那么它将非常有用。在Google上搜索时,我只能获得Windows RT API。

+0

您引用的类型仅适用于Windows应用商店应用。您可能能够关注[这些](http://www.wintellect.com/CS/blogs/jeffreyr/archive/2011/09/20/using-the-windows-runtime-from-a-non-metro- application.aspx)手动添加引用和构建的说明,但我没有经验。 –

+0

实际上[这篇文章](http://software.intel.com/en-us/articles/geo-location-on-windows-8-desktop-applications-using-winrt)声称它非常简单。我还没有测试过。我将在未来两周内做更多的研究。 – Bart

回答

3

要解决您的错误,您必须参考Bart在问题的评论中给出的link

You might need to add a reference to System.Runtime.WindowsRuntime.dll as well if you are using mapped types like Windows Runtime event handlers:

...

That assembly resides in C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETCore\v4.5

最近,我发现了一个 “解决方案” 为一个类似的问题:C# desktop application doesn't share my physical location。也许你可能对我的方法感兴趣:https://stackoverflow.com/a/14645837/674700

它更像是一种解决方法,它并不针对Windows 8,但它最终工作。

+1

为我工作..谢谢.. !! –

2

alex's solution works! 添加引用和地理位置API开始工作就像一个魅力!所以做其他传感器的异步方法!

这里是我刚开始使用它的一个功能。

async public void UseGeoLocation() 
{ 
    Geolocator _GeoLocator = new Geolocator(); 
    Geoposition _GeoPosition = 
     await _GeoLocator.GetGeopositionAsync(); 

    Clipboard.Clear(); 
    Clipboard.SetText("latitude," + 
     _GeoPosition.Coordinate.Latitude.ToString() + 
     "," + "longitude," + _GeoPosition.Coordinate.Longitude.ToString() + 
     "," + "heading," + _GeoPosition.Coordinate.Heading.ToString() + 
     "," + "speed," + _GeoPosition.Coordinate.Speed.ToString()); 

    Application.Exit(); 
} 
相关问题