簡體   English   中英

FromBluetoothAddressAsync IAsyncOperation 不包含“GetAwaiter”錯誤的定義

[英]FromBluetoothAddressAsync IAsyncOperation does not contain a definition for 'GetAwaiter' error

我正在開發一個簡單的 BLE UWP。 我一直在指“ 發現后 Windows UWP 連接到 BLE 設備”,在 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應該返回一個任務。

從“ BluetoothLEDevice Class ”,我可以檢查FromBluetoothAddressAsync方法是否具有以下簽名:

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

這意味着它返回IAsyncOperation<BluetoothLEDevice> 在我看來,這似乎足以被認為是Task<>

問題是由於鏈接斷開導致IAsyncOperation<>被識別為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 包,它會為您完成這項工作。

注意:特別是GetAwaiterSystem命名空間中的擴展,可以從這些引用中獲得(您仍然需要using System; - 確保您沒有從文件中刪除它)。 擴展信息位於 MSDN - WindowsRuntimeSystemExtensions.GetAwaiter 上

對於其他一些 UWP 操作,只需using System添加:

using System;

//...

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM