我是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桌面应用程序中的位置
- Unity Shader实现模糊效果 2023-04-27
- 如何使用C# 捕获进程输出 2023-03-10
- c# 模拟线性回归的示例 2023-03-14
- C# 使用Aspose.Cells 导出Excel的步骤及问题记录 2023-05-16
- Oracle中for循环的使用方法 2023-07-04
- 在C# 8中如何使用默认接口方法详解 2023-03-29
- .NET CORE DI 依赖注入 2023-09-27
- user32.dll 函数说明小结 2022-12-26
- Unity3D实现渐变颜色效果 2023-01-16
- WPF使用DrawingContext实现绘制刻度条 2023-07-04