调度员'没有包含'InvokeAsync'的定义,也没有扩展方法'InvokeAsync'

调度员'没有包含'InvokeAsync'的定义,也没有扩展方法'InvokeAsync'

问题描述:

请问我有错误,这是我的代码。调度员'没有包含'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同样的问题先生... –

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,它工作得很好.. –