簡體   English   中英

從桌面應用程序使用WinRT時出現C#“等待”錯誤

[英]C# “await” error when using WinRT from Desktop app

我試圖在Windows#中使用C#從桌面應用程序獲取GPS位置。

我遵循此操作方法來設置Visual Studio和此代碼示例,以將以下簡單代碼創建為C#控制台應用程序:

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” ...

我想念什么?

感謝您的回答,

嗶嘰

我終於找到了我的問題:

  • 我使用的是8.1,因此我在.csproj文件(targetingPlatform)中對此進行了更改,而不是針對8.0
  • 修改之后,我可以引用“ Windows”
  • 我手動引用了以下兩個DLL:

    C:\\ Program Files(x86)\\參考程序集\\ Microsoft \\ Framework.NETFramework \\ v4.5 \\ Facades \\ System.Runtime.dll

    C:\\ WINDOWS \\ Microsoft.NET \\ Framework64 \\ v4.0.30319 \\ System.Runtime.WindowsRuntime.dll

如果您使用的是Win8.1,請使用:

<TargetPlatformVersion>8.1</TargetPlatformVersion>

而不是8.0。

添加到現有答案中:

我突然在WPF中使用多項目解決方案遇到了這個問題(仍然不知道為什么)。 我嘗試在TargetPlatformVersions 8.1和10.0之間切換,嘗試使用v4.5-和v4.5.1-System.Runtime.WindowsRuntime.dll,始終在BadImageFormatException和FileNotFoundException之間交替。

原來是某些app.config文件中的引用(不是.csproj文件!)。 Visual Studio 2017必須在某個時候添加了它們,並且一旦我刪除了這些條目,上述解決方案便開始起作用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM