FromBluetoothAddressAsync IAsyncOperation 不包含“GetAwaiter"错误的定义 [英] FromBluetoothAddressAsync IAsyncOperation does not contain a definition for 'GetAwaiter' error

查看:54
本文介绍了FromBluetoothAddressAsync IAsyncOperation 不包含“GetAwaiter"错误的定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个简单的 BLE UWP.我一直在指Windows UWP 在发现后连接到 BLE 设备",在 Visual Studio 2017 中工作.

I am working on a simple BLE UWP. I've been referring to "Windows UWP connect to BLE device after discovery", working in 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 的引用,但仍然出现此错误.

I tried adding references to System.Runtime, System.Runtime.WindowsRuntime, and Windows but this error still appears.

根据我的搜索,原因似乎是方法 FromBluetoothAddressAsync 应该返回一个任务.

From my searching, the reason seems to be that the method FromBluetoothAddressAsync should return a Task.

来自BluetoothLEDevice Class",我可以检查 FromBluetoothAddressAsync 方法是否具有此签名:

From "BluetoothLEDevice Class", I can check that FromBluetoothAddressAsync method has this signature:

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

这意味着它返回IAsyncOperation.在我看来,这似乎足以被视为Task<>.

which means that it returns IAsyncOperation<BluetoothLEDevice>. The way I see it, this seems enough to be recognized as something as a Task<>.

问题是否是由于链接断开导致IAsyncOperation<> 被识别为Task<> 的子项?

Is the problem due to a broken link which allows IAsyncOperation<> to be recognized as a child of Task<>?

推荐答案

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

To await an IAsyncOperation, you need two things:

  • C:Program Files (x86)Reference AssembliesMicrosoftFramework.NETCorev4.5System.Runtime.WindowsRuntime.dll 的引用
  • C:Program Files (x86)Windows Kits10UnionMetadataFacadeWindows.WinMD 的引用

如果缺少任何一个引用,那么它将不起作用.您还可以使用 UwpDesktop nuget 包,它会为您完成这项工作.

If either reference is missing then it won't work. You can also use the UwpDesktop nuget package, that will do the work for you.

注意:特别是 GetAwaiterSystem 命名空间中的扩展,可以从这些引用中获得(你仍然需要 using System; - 确保你尚未将其从文件中删除).扩展信息在 MSDN 上 - WindowsRuntimeSystemExtensions.GetAwaiter.

Note: specifically GetAwaiter is extension in System namespace that is available from those references (you still need using System; - make sure you have not removed it from the file). The extension info is on MSDN - WindowsRuntimeSystemExtensions.GetAwaiter.

这篇关于FromBluetoothAddressAsync IAsyncOperation 不包含“GetAwaiter"错误的定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆