如何使用 await 语法从 Windows.Devices.Enumeration.DeviceInformation

本文关键字:Devices Enumeration DeviceInformation Windows 何使用 await 语法 | 更新日期: 2023-09-27 17:56:56

我的计算机有一个可以工作的智能卡读卡器,应该由下面的代码片段返回:

string selector = Windows.Devices.SmartCards.SmartCardReader.GetDeviceSelector();
Windows.Devices.Enumeration.DeviceInformationCollection devices = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(selector);

但是,我收到此错误:

错误 CS4036 ' IAsyncOperation<DeviceInformationCollection> ' 不包含 ' GetAwaiter' 的定义,并且找不到接受类型为 ' IAsyncOperation<DeviceInformationCollection>' 的第一个参数的扩展方法' GetAwaiter ' (您是否缺少 ' System' 的 using 指令?

代码片段是从Microsoft的 C# 示例代码中复制的

我该怎么做才能解决此错误?

如何使用 await 语法从 Windows.Devices.Enumeration.DeviceInformation

错误消息为您提供了有关问题所在的一个很好的提示。将 using System; 指令添加到文件顶部。

为了将来参考,您可以通过在 MSDN 中搜索有问题的扩展方法(例如搜索"IAsyncOperation GetAwaiter 扩展方法")来自行跟踪此问题。当我这样做时,第一页是WindowsRuntimeSystemExtensions.GetAwaiter方法(IAsyncOperation),在该页上,您可以看到该方法位于System命名空间中,并在System.Runtime.WindowsRuntime.dll程序集中定义。使用这两个信息,您可以仔细检查以确保.cs文件中有using System;,并且项目中具有System.Runtime.WindowsRuntime.dll程序集引用。