调度员'没有包含'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
我迷路了,请,我需要这些帮助。谢谢。
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);
});
谢谢@KobyDuck,它工作得很好.. –
写入它作为''_comboBox.Dispatcher.InvokeAsync((动作)(()=> ContentChanged?.Invoke(sender,EventArgs.Empty)));'' –
@RandRandom同样的问题先生... –