如何使用等待语法从Windows.Devices.Enumeration.DeviceInformation.FindAllAsync中读取结果?

时间:2016-07-14 02:45:10

标签: c# async-await winmd

我的电脑有一个可用的智能卡读卡器,应由下面的代码段返回:

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

但是,我收到此错误:

  

错误CS4036&#39; IAsyncOperation<DeviceInformationCollection>&#39;不包含&#39; GetAwaiter&#39;的定义没有扩展方法&#39; GetAwaiter&#39;接受类型&#39; IAsyncOperation<DeviceInformationCollection>&#39;的第一个参数。可以找到(你是否错过了&#39; System&#39;的使用指令?)

Microsoft's C# Sample Code

复制代码段

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

1 个答案:

答案 0 :(得分:3)

错误消息为您提供了一个关于错误的非常好的提示。将using System;指令添加到文件顶部。

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