C#“等待"从桌面应用程序使用 WinRT 时出错 [英] C# "await" error when using WinRT from Desktop app

查看:21
本文介绍了C#“等待"从桌面应用程序使用 WinRT 时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用 C# 中的 Windows 运行时从桌面应用获取我的 GPS 位置.

I trying to get my GPS position from a Desktop app, using Windows Runtime, in C#.

我按照 这个方法 进行设置使用 Visual Studio 和 此代码示例 创建以下内容作为 C# 控制台应用程序的简单代码:

I followed this how-to to set up Visual Studio and this code sample to create the following trivial code as a C# console app :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

using System.IO;
using System.Runtime;
using System.Windows;

using Windows;
using Windows.Devices.Geolocation;
using Windows.Foundation;
using Windows.Foundation.Collections;

namespace GeoLocCS
{
    public sealed partial class Program
    {
        static void Main(string[] args)
        {
            Test test = new Test();
            Console.Read(); 
        }
    }

    public class Test
    {
        private Geolocator geolocator;

        public Test()
        {
            Console.WriteLine("test");
            geolocator = new Geolocator();

            this.getPos();
        }

        async void getPos()
        {
            Geoposition pos = await geolocator.GetGeopositionAsync();
            Console.WriteLine(pos.Coordinate.Latitude.ToString());
        }
    }
}

尝试编译时出现错误:

Error 1 'await' requires that the type 'Windows.Foundation.IAsyncOperation<Windows.Devices.Geolocation.Geoposition>' have a suitable GetAwaiter method. Are you missing a using directive for 'System'?

我尝试引用每个可以引用的 DLL,例如System.runtime"、System.Runtime.WindowsRuntime"、Windows.Foundation"...

I tried to reference every DLL that I could, like "System.runtime", "System.Runtime.WindowsRuntime", "Windows.Foundation"...

我错过了什么?

谢谢你的回答,

谢尔盖

推荐答案

我终于找到了我的问题:

I finally figured out my problem :

  • 我使用的是 8.1,所以我在 .csproj 文件 (targetingPlatform) 中更改了这个,而不是针对 8.0
  • 修改后,我可以引用Windows"
  • 我手动引用了以下两个 DLL:

  • I am on 8.1 so I changed this in the .csproj file (targetingPlatform) instead of targeting 8.0
  • After that modification, I could reference "Windows"
  • I manually referenced the two following DLLs :

C:Program Files (x86)Reference AssembliesMicrosoftFramework.NETFrameworkv4.5FacadesSystem.Runtime.dll

C:WindowsMicrosoft.NETFramework64v4.0.30319System.Runtime.WindowsRuntime.dll

这篇关于C#“等待"从桌面应用程序使用 WinRT 时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆