在Windows 8桌面应用程序中获取位置 [英] Getting location in Windows 8 desktop apps
问题描述
我是C#的初学者,但是我经常使用Java。我正在尝试在我的应用中使用以下代码来获取位置数据。我正在制作一个Windows 8桌面应用程序以在设备中使用GPS传感器:
I am a total beginner at C#, but I have used Java a lot. I am trying to use the following code in my app to get location data. I am making a Windows 8 desktop app to use the GPS sensor in my device:
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'要求类型
'Windows。 Foundation.IAsyncOperation'
具有合适的GetAwaiter方法。您是否缺少针对系统的使用指令
? C:\Users\clidy\Documents\Visual Studio
2012\Projects\Hello-Location\Hello-Location\Form1.cs
'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 桌面应用的传感器API。谷歌搜索后,我只得到Windows RT API。
Also it will be very useful if you can point me to some resources for C# location and the sensor API for windows desktop apps. Upon googling, I am only getting Windows RT APIs.
推荐答案
要解决您的错误,您必须引用链接,是Bart在问题的评论之一中给出的。
To fix your error you have to reference to the link that Bart gave in one of the question's comments.
如果使用的是Windows Runtime事件
处理程序之类的映射类型,则可能还需要添加对System.Runtime.WindowsRuntime.dll
的引用:
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:
...
该程序集位于C:\程序文件(x86)\参考
程序集\Microsoft\Framework.NETCore\ 4.5v4.5
That assembly resides in C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETCore\v4.5
我最近找到了一个 解决方案提出类似问题: C#桌面应用程序没有分享我的地理位置。也许您可能对我的方法感兴趣: https://stackoverflow.com/a/14645837/674700 。
I recently found a "solution" for a similar question: C# desktop application doesn't share my physical location. Maybe you might be interested by my approach: https://stackoverflow.com/a/14645837/674700.
这更像是一种解决方法,它的目标不是Windows 8,但最终还是可以使用。
It's more like a workaround and it's not targeting Windows 8, but it works in the end.
这篇关于在Windows 8桌面应用程序中获取位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!