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

查看:744
本文介绍了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应该返回一个Task.

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

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

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

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

表示它返回IAsyncOperation<BluetoothLEDevice>.从我的角度来看,这似乎足以被识别为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 Assemblies\Microsoft\Framework\.NETCore\v4.5\System.Runtime.WindowsRuntime.dll
  • 的引用
  • C:\Program Files (x86)\Windows Kits\10\UnionMetadata\Facade\Windows.WinMD
  • 的引用
  • A reference to C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETCore\v4.5\System.Runtime.WindowsRuntime.dll
  • A reference to C:\Program Files (x86)\Windows Kits\10\UnionMetadata\Facade\Windows.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天全站免登陆