我将同一段代码从一个项目复制到另一个项目,在第一个项目中它编译正常,但在第二个项目中却没有。
作品是:
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 代码:
更多信息 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/