C#“等待"从桌面应用程序使用 WinRT 时出错

2

本文介绍了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 时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

C# 中的多播委托奇怪行为?
Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)...
2023-11-11 C#/.NET开发问题
6

参数计数与调用不匹配?
Parameter count mismatch with Invoke?(参数计数与调用不匹配?)...
2023-11-11 C#/.NET开发问题
26

如何将代表存储在列表中
How to store delegates in a List(如何将代表存储在列表中)...
2023-11-11 C#/.NET开发问题
6

代表如何工作(在后台)?
How delegates work (in the background)?(代表如何工作(在后台)?)...
2023-11-11 C#/.NET开发问题
5

没有 EndInvoke 的 C# 异步调用?
C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)...
2023-11-11 C#/.NET开发问题
2

Delegate.CreateDelegate() 和泛型:错误绑定到目标方法
Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)...
2023-11-11 C#/.NET开发问题
14