小编Kid*_*Liu的帖子

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

我正在桌面应用程序中编写代码,该应用程序使用适用于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上的桌面应用程序仍然有效.

我该如何解决这个错误?

c# desktop-application async-await windows-runtime win-universal-app

4
推荐指数
1
解决办法
3170
查看次数