2017-09-02 110 views
0

請問我有錯誤,這是我的代碼。調度員'沒有包含'InvokeAsync'的定義,也沒有擴展方法'InvokeAsync'

private void ComboBoxSelectionChanged(object sender, SelectionChangedEventArgs e) 
     { 
      _comboBox.Dispatcher.InvokeAsync(() => ContentChanged?.Invoke(sender, EventArgs.Empty)); 
     } 

其說Dispatcher' does not contain a definition for 'InvokeAsync' and no extension method 'InvokeAsync' accepting a first argument of type 'Dispatcher' could be found (are you missing a using directive or an assembly reference? wpf我迷路了,請,我需要這些幫助。謝謝。

+0

寫入它作爲''_comboBox.Dispatcher.InvokeAsync((動作)(()=> ContentChanged?.Invoke(sender,EventArgs.Empty)));'' –

+0

@RandRandom同樣的問題先生... –

回答

3

Dispatcher.InvokeAsync絕對是.NET 4.5以來的現有方法。如果您嘗試編譯.NET 4.0或更早版本,您會看到該錯誤。

它的效果與您撥打Dispatcher.BeginInvoke的效果相同。不同的是BeginInvoke接受委託(需要從lambda轉換),而InvokeAsync不接受,因爲它接受Action。這樣做是爲了重構API,但是這種方式不會破壞仍然使用BeginInvoke的代碼。有關更多詳情,請參閱this thread

之前.NET 4.5

_comboBox.Dispatcher.BeginInvoke((Action)(() => { 
    ContentChanged?.Invoke(sender, EventArgs.Empty); 
})); 

由於.NET 4.5

_comboBox.Dispatcher.InvokeAsync(() => { 
    ContentChanged?.Invoke(sender, EventArgs.Empty); 
}); 
+0

謝謝@KobyDuck,它工作得很好.. –

相關問題