C#从桌面应用程序中使用的WinRT时,“等待”错误(C# “await” error when u

2019-11-03 00:58发布

我想从一个桌面应用我的GPS位置,使用Windows运行时,在C#。

我跟着这个如何对建立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” ...

我很想念?

感谢您的回答,

哔叽

Answer 1:

我终于想通了,我的问题:

  • 我在8.1,所以我在.csproj的文件(targetingPlatform)改变了这个,而不是目标8.0
  • 这一修改后,我可以引用“窗口”
  • 我手动参考以下两个DLL文件:

    C:\ Program Files文件(x86)的\参考大会\微软\ Framework.NETFramework \ V4.5 \外立面\ System.Runtime.dll

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



Answer 2:

如果你是在Win8.1请使用:

<TargetPlatformVersion>8.1</TargetPlatformVersion>

而不是8.0。



Answer 3:

添加到现有的答案:

我突然接到了这个问题,在WPF(仍然不知道为什么)多项目的解决方案。 我试图TargetPlatformVersions 8.1和10.0之间的切换,使用v4.5-和V4.5.1-System.Runtime.WindowsRuntime.dll,总是BadImageFormatException和FileNotFoundException异常之间交替尝试。

原来是一些在app.config-文件的引用(不.csproj的档案!)。 Visual Studio的2017年必须在某一点增加了他们,只要我删除这些条目,上述解决方案终于奏效。



文章来源: C# “await” error when using WinRT from Desktop app