c# - 错误 : await requires that the type have a suitable GetAwaiter method

标签 c# .net bluetooth windows-runtime async-await

我将同一段代码从一个项目复制到另一个项目,在第一个项目中它编译正常,但在第二个项目中却没有。

作品是:

using Windows.Devices.Bluetooth;
// a couple of lines of other code
var rfcommProvider = await RfcommServiceProvider.CreateAsync(RfcommServiceId.FromUuid(RfcommChatServiceUuid));

错误:

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

第一个项目是Microsoft创建的demo,它是一个Windows 8.1应用程序。

我的应用程序是一个普通的 WPF 应用程序,它面向 .NET 4.5.1 和 Windows 8.1,我添加了对 Windows.winmd 的引用,如 here 所述

我不知道为什么当引用、命名空间等所有常见事物都正常时,同一段代码可能会在一个应用程序中编译而不能在另一个应用程序中编译。我唯一的想法是,也许我正在引用一些尚未等待的旧版本,但我不知道如何验证这一点。

最佳答案

错误是 IAsyncOperation 没有 GetAwaiter 方法。它存在于 System.Runtime.WindowsRuntime.dll 中,您需要将其添加为引用,因为您在 .Net 环境中使用 WinRT 代码:

System.Runtime.WindowsRuntime.dll

更多信息 How to use specific WinRT API from Desktop apps .
Reference 1 Reference 2

关于c# - 错误 : await requires that the type have a suitable GetAwaiter method,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27758796/

相关文章:

c# - ConfigureAwait 和 GetAwaiter 改变行为

c# - 从 Obj C 到 C# 的转换

ios - 蓝牙通信可能性

android - kitkat 上的 hfp 客户端角色实现

android - 蓝牙Android SPP,串行发送命令到设备?

c# - ASP.net 应用程序中 JTable 的更新、删除、编辑操作出错,而选择工作正常

c# - 如何在netcore1.1项目上集成Google OAuth2 MVC?

c# - 在 C# 中运行 socket.io 服务器

.net - Argotic Syndication Framework 诉 System.ServiceModel.Syndication

c# - 有没有办法在您单击菜单时阻止菜单关闭?