C# FromBluetoothAddressAsync IAsyncOperation不包含';GetWaiter';错误
我正在研究一个简单的BLE UWP。我指的是2017年在Visual Studio工作的“” 我的核心代码是:C# FromBluetoothAddressAsync IAsyncOperation不包含';GetWaiter';错误,c#,bluetooth,async-await,C#,Bluetooth,Async Await,我正在研究一个简单的BLE UWP。我指的是2017年在Visual Studio工作的“” 我的核心代码是: 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 Sy
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<Bluetooth> does not contain a definition for
'GetAwaiter' and the best extension method overload
'windowsRuntimeSystemExtensions.GetAwaiter(IAsyncAction)' requires a
receiver of type 'IAsyncAction'
这意味着它返回IAsyncOperation
。在我看来,这似乎足以被视为一项任务
问题是否是由于断开的链接导致
IAsyncOperation
被识别为任务的子任务?要等待IAsyncOperation
,您需要两件事:
- 对
C:\Program Files(x86)\reference Assembly\Microsoft\Framework\.NETCore\v4.5\System.Runtime.WindowsRuntime.dll的引用
- 对
C:\Program Files(x86)\Windows Kits\10\UnionMetadata\Facade\Windows.WinMD的引用
如果缺少任何一个引用,则它将不起作用。您还可以使用nuget包,它将为您完成这项工作
注意:特别是GetAwaiter
是System
命名空间中的扩展名,可从这些引用中获得(您仍然需要使用System;
-确保未将其从文件中删除)。扩展信息位于MSDN-。对于其他一些UWP操作,只需使用系统添加:
using System;
//...
// example - getting file reference:
var file = await Windows.Storage.ApplicationData.Current.LocalFolder.GetFileAsync("myFile.txt);
对于“.NET标准2.0”库项目,添加以下NUGET:
非常感谢您。”UwpDesktop的解决方案对我来说很有效。。现在弹出了一个不同的错误,但至少等待错误没有出现。我担心build 15063的代码永远无法通过对BluetoothLEDevice的调用。FromBluetoothAddressAsync
另一个aswer是一个真正的答案,我的最终成功了。System.Runtime.WindowsRuntime做到了这一点!谢谢
using System;
//...
// example - getting file reference:
var file = await Windows.Storage.ApplicationData.Current.LocalFolder.GetFileAsync("myFile.txt);