在 Windows 8 桌面应用程序中获取位置

Getting location in Windows 8 desktop apps(在 Windows 8 桌面应用程序中获取位置)

本文介绍了在 Windows 8 桌面应用程序中获取位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 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 方法.您是否缺少 using 指令为系统"?C:Usersclidydocumentsvisual studio2012ProjectsHello-LocationHello-LocationForm1.cs

'await' requires that the type 'Windows.Foundation.IAsyncOperation' have a suitable GetAwaiter method. Are you missing a using directive for 'System'? C:Usersclidydocumentsvisual studio 2012ProjectsHello-LocationHello-LocationForm1.cs

我该如何解决这个问题?

How can I fix this?

此外,如果您可以向我指出一些 C# 位置资源和适用于 windows desktop 应用程序的传感器 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.

推荐答案

要修复您的错误,您必须参考 link Bart 在其中一个问题的评论中给出的链接.

To fix your error you have to reference to the link that Bart gave in one of the question's comments.

您可能需要添加对 System.Runtime.WindowsRuntime.dll 的引用如果您使用的是映射类型,例如 Windows 运行时事件处理程序:

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:Program Files (x86)Reference程序集MicrosoftFramework.NETCorev4.5

That assembly resides in C:Program Files (x86)Reference AssembliesMicrosoftFramework.NETCorev4.5

我最近找到了一个解决方案";对于类似的问题:C# 桌面应用程序没有't 分享我的实际位置.也许您可能对我的方法感兴趣: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 桌面应用程序中获取位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在 Windows 8 桌面应用程序中获取位置

基础教程推荐