2017-05-21 94 views
1

我正在研究一個簡單的ble uwp。我已經提到這個codeFromBluetoothAddressAsync IAsyncOperation不包含'GetAwaiter'錯誤的定義

我在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不包含 定義'GetAwaiter'和最佳擴展方法過載 '窗口RuntimeSystemExtensions.GetAwaiter(IAsyncAction)」需要類型的 接收器‘IAsyncAction’

我已經嘗試添加引用‘System.Runtime’,‘System.Runtime.WindowsRuntime’,‘視窗’,但這個錯誤仍出現。

從我的谷歌搜索來看,原因似乎是FromBluetoothAddressAsync方法應該返回一個任務。

msdn,我可以檢查 'FromBluetoothAddressAsync' 方法具有以下的結構

公共靜態IAsyncOperation FromBluetoothAddressAsync(UINT64 bluetoothAddress, BluetoothAddressType bluetoothAddressType)

這意味着,它返回IAsyncOperation。我看到它的方式似乎足以被認定爲任務<>。

是否由於鏈接斷開而導致sIAsyncOperation <>被識別爲任務<>的子節點?

我希望有任何幫助解決這個錯誤。

回答

3

以等待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軟件包,它將爲您完成工作。

+0

非常感謝。 'UwpDesktop'解決方案爲我工作..現在彈出一個不同的錯誤,但至少await錯誤不顯示。 – kwagjj

+1

恐怕使用構建15063代碼永遠不會通過對'''BluetoothLEDevice.FromBluetoothAddressAsync'''的調用 – abinop

相關問題