c# - 等待 DeviceInformation.FindAllAsync(selector) 并引用 System.Runtime.WindowsRuntime 时出错

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

我正在桌面应用程序中编写代码,该应用程序发现并连接到具有适用于 Windows 10 的 Wi-Fi Direct 的设备。我遵循了 instructions关于如何从非 WinRT 环境调用 Windows 运行时 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
    {
        ...
    }
}

错误依旧

'await' requires that the type 'Windows.Foundation.IAsyncOperation<Windows.Devices.Enumeration.DeviceInformationCollection>' have a suitable GetAwaiter method. Are you missing a using directive for 'System'?

具有以下库引用: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

),那么我认为该 hack 对 Windows 10 上的桌面应用程序仍然有效。

如何修复此错误?

最佳答案

非常感谢,@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.dll,属于 C:\Windows\Microsoft.NET\Framework\v4.0.30319\System.Runtime.WindowsRuntime.dll
  • Windows,位于 C:\Program Files (x86)\Windows Kits\10\UnionMetadata\Windows.winmd

现在它开始工作了。

关于c# - 等待 DeviceInformation.FindAllAsync(selector) 并引用 System.Runtime.WindowsRuntime 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33148825/

相关文章:

c# - 如何在wpf中获取文本框的绑定(bind)

windows - 从开始屏幕而不是桌面运行 Delphi 应用程序需要什么 API

c# - 将 onCallback 转换为异步

c# - 在 Windows Phone 8.1 上获取 CivicAddress

c# - Win RT SQLite 事务性插入

c# - 在库中编写同步和异步方法并保持 DRY 的模式

c# - 始终使用await (`await x; await x; ....` ) vs .Result 在第一次await (`await x; x.Result; x.Result; ....` )之后?

c# - 如何防止 richTextBox 在其中粘贴图像?

c# - 使用文件

c# - 标签 TapGesture 不会触发 Xamarin Forms