引用System.Runtime.WindowsRuntime时等待DeviceInformation.FindAllAsync(selector)的错误

Kid*_*Liu 4 c# desktop-application async-await windows-runtime win-universal-app

我正在桌面应用程序中编写代码,该应用程序使用适用于Windows 10的Wi-Fi Direct发现并连接到设备.我已按照说明操作如何从非WinRT环境调用Windows Runtime API,VS 2013上的一切都很好,除了以下几行,我将它们移动到面向.NET Framework 4.5的控制台应用程序中:

using System;

using Windows.Devices.Enumeration;
using Windows.Devices.WiFiDirect;

static void Main()
{
    DoSomethingAsync().Wait();
}

static async void DoSomethingAsync()
{
    var selector = WiFiDirectDevice.GetDeviceSelector(WiFiDirectDeviceSelectorType.DeviceInterface);
    foreach (var info in await DeviceInformation.FindAllAsync(selector)) // <-- error
    {
        ...
    }
}
Run Code Online (Sandbox Code Playgroud)

错误仍然存​​在

'await'要求类型' Windows.Foundation.IAsyncOperation<Windows.Devices.Enumeration.DeviceInformationCollection>'具有合适的GetAwaiter方法.你错过了'系统'的使用指令吗?

以下库参考:image

  • System.Runtime,C:\ Program Files(x86)\ Reference Assemblies\Microsoft\Framework.NETFramework\v4.5\Facades\System.Runtime.dll
  • System.Runtime.WindowsRuntime,C:\ Program Files(x86)\ Reference Assemblies\Microsoft\Framework.NETCore\v4.5\System.Runtime.WindowsRuntime.dll
  • Windows.Devices,C:\ Windows\System32\WinMetadata\Windows.Devices.winmd
  • Windows.Foundation,C:\ Windows\System32\WinMetadata\Windows.Foundation.winmd

我确信selector可以正确检索(类似于

System.Devices.InterfaceClassGuid:="{439B20AF-8955-405B-99F0-A62AF0C68D43}" 
AND System.Devices.InterfaceEnabled:=System.StructuredQueryType.Boolean#True
Run Code Online (Sandbox Code Playgroud)

),然后我认为黑客对Windows 10上的桌面应用程序仍然有效.

我该如何解决这个错误?

Kid*_*Liu 6

非常感谢@Yuval Itzchakov,@ vidalsasoon

事实证明我被System.Runtime.WindowsRuntime.dll找到的路径误导了.在C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETCore\v4.5库中实际上是.NET Framework 4.0,其中没有定义这样的扩展方法(GetWaiter,AsTask...).但是,当您在VS中引用.NET Framework程序集时,看起来该文件夹是默认位置.

然后我引用了

  • System.Runtime.WindowsRuntime.dllC:\Windows\Microsoft.NET\Framework\v4.0.30319\System.Runtime.WindowsRuntime.dll
  • WindowsC:\Program Files (x86)\Windows Kits\10\UnionMetadata\Windows.winmd

现在它正在发挥作用.