gpt4 book ai didi

c# - Dispatcher' 不包含 'InvokeAsync' 的定义并且没有扩展方法 'InvokeAsync'

转载 作者:行者123 更新时间:2023-12-03 10:54:21 26 4
gpt4 key购买 nike

请我有错误,这是我的代码。

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接受委托(delegate)(需要从 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);
});

关于c# - Dispatcher' 不包含 'InvokeAsync' 的定义并且没有扩展方法 'InvokeAsync',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46019173/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com