【问题标题】:FromBluetoothAddressAsync IAsyncOperation does not contain a definition for 'GetAwaiter' errorFromBluetoothAddressAsync IAsyncOperation 不包含“GetAwaiter”错误的定义
【发布时间】:2026-01-04 07:30:01
【问题描述】:

我正在开发一个简单的 BLE UWP。我一直指的是“Windows UWP connect to BLE device after discovery”,在 Visual Studio 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<Bluetooth> does not contain a definition for
'GetAwaiter' and the best extension method overload
'windowsRuntimeSystemExtensions.GetAwaiter(IAsyncAction)' requires a
receiver of type 'IAsyncAction'

我尝试添加对System.RuntimeSystem.Runtime.WindowsRuntimeWindows 的引用,但仍然出现此错误。

从我的搜索来看,原因似乎是FromBluetoothAddressAsync方法应该返回一个Task。

从“BluetoothLEDevice Class”,我可以检查FromBluetoothAddressAsync 方法有这个签名:

public static IAsyncOperation<BluetoothLEDevice> FromBluetoothAddressAsync(
    UInt64 bluetoothAddress,
    BluetoothAddressType bluetoothAddressType
)

这意味着它返回IAsyncOperation&lt;BluetoothLEDevice&gt;。在我看来,这似乎足以被视为Task&lt;&gt;

问题是由于链接断开导致IAsyncOperation&lt;&gt; 被识别为Task&lt;&gt; 的孩子吗?

【问题讨论】:

    标签: c# bluetooth async-await


    【解决方案1】:

    要等待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 包,它会为您完成工作。

    注意:特别是 GetAwaiterSystem 命名空间中的扩展名,可从这些引用中获得(您仍然需要 using System; - 确保您没有从文件中删除它)。扩展信息在 MSDN -WindowsRuntimeSystemExtensions.GetAwaiter

    【讨论】:

    • 非常感谢。 'UwpDesktop' 解决方案对我有用.. 现在弹出一个不同的错误,但至少等待错误没有出现。
    • 恐怕构建 15063 代码永远无法通过对 BluetoothLEDevice.FromBluetoothAddressAsync 的调用
    • 另一个答案是真正的答案
    • System.Runtime.WindowsRuntime nuget 包对我有用。
    • System.Runtime.WindowsRuntime 成功了!谢谢
    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】: