gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 00:23:53 31 4
gpt4 key购买 nike

我正在桌面应用程序中编写代码,该应用程序发现并连接到具有适用于 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/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com