我是C#的初学者,但我经常使用Java.我想在我的应用程序中使用以下代码来获取位置数据.我正在制作一个Windows 8桌面应用程序,以便在我的设备中使用GPS传感器:using System;using System.Collections.Generic;using ...

我是C#的初学者,但我经常使用Java.我想在我的应用程序中使用以下代码来获取位置数据.我正在制作一个Windows 8桌面应用程序,以便在我的设备中使用GPS传感器:
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桌面应用程序的传感器API,那么它将非常有用.谷歌搜索时,我只获得Windows RT API.
解决方法:
要解决您的错误,您必须参考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,但它最终会起作用.
本文标题为:c# – 获取Windows 8桌面应用程序中的位置


基础教程推荐
- C#调用摄像头实现拍照功能的示例代码 2023-03-09
- C#获取指定目录下某种格式文件集并备份到指定文件夹 2023-05-30
- C#通过标签软件Bartender的ZPL命令打印条码 2023-05-16
- C#中 Json 序列化去掉null值的方法 2022-11-18
- C#中的Linq to JSON操作详解 2023-06-08
- 实例详解C#实现http不同方法的请求 2022-12-26
- Unity 如何获取鼠标停留位置下的物体 2023-04-10
- c# – USING块在网站与Windows窗体中的行为不同 2023-09-20
- Unity shader实现高斯模糊效果 2023-01-16
- C# 解析XML和反序列化的示例 2023-04-14