FromBluetoothAddressAsync IAsyncOperation不包含“GetAwaiter”错误的定义

我正在研究一个简单的流行语。 我一直在提到这段代码

我在视觉工作室2017工作。

我的核心代码如下:

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Windows.Threading; using Windows.Devices.Bluetooth.Advertisement; using Windows.Devices.Bluetooth; using Windows.Devices; using Windows.Foundation; using Windows; namespace WindowsFormsApp2 { public partial class Form1 : Form { private BluetoothLEAdvertisementWatcher watcher; public Form1() { InitializeComponent(); watcher = new BluetoothLEAdvertisementWatcher(); watcher.Received += OnAdvertisementReceived; } private async void OnAdvertisementReceived(BluetoothLEAdvertisementWatcher watcher, BluetoothLEAdvertisementReceivedEventArgs eventArgs) { var dev = await BluetoothLEDevice.FromBluetoothAddressAsync(eventArgs.BluetoothAddress); } } 

在行var dev = await BluetoothLEDevice.FromBluetoothAddressAsync(eventArgs.BluetoothAddress)它给出了错误

IAsyncOperation不包含’GetAwaiter’的定义,最好的扩展方法重载’windowsRuntimeSystemExtensions.GetAwaiter(IAsyncAction)’需要一个’IAsyncAction’类型的接收器

我已尝试添加对’System.Runtime’,’System.Runtime.WindowsRuntime’,’Windows’的引用,但仍会出现此错误。

从我的谷歌搜索,原因似乎是’FromBluetoothAddressAsync’方法应该返回一个任务。

从msdn ,我可以检查’FromBluetoothAddressAsync’方法具有以下结构

public static IAsyncOperation FromBluetoothAddressAsync(UInt64 bluetoothAddress,BluetoothAddressType bluetoothAddressType)

这意味着它返回IAsyncOperation。 我看待它的方式,这似乎足以被认为是一个任务。

问题是由于链接断开导致sIAsyncOperation 被识别为Task 的子级吗?

我将不胜感激任何帮助来解决这个错误。

要等待IAsyncOperation ,您需要两件事:

  • C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETCore\v4.5\System.Runtime.WindowsRuntime.dll
  • C:\Program Files (x86)\Windows Kits\10\UnionMetadata\Facade\Windows.WinMD

如果缺少任何一个参考,那么它将无法工作。 您也可以使用UwpDesktop nuget包,它将为您完成工作。

对于其他一些UWP操作,只需using System添加:

 using System; //... // example - getting file reference: var file = await Windows.Storage.ApplicationData.Current.LocalFolder.GetFileAsync("myFile.txt);