无法将匿名方法转换为类型“ System.Windows.Threading.DispatcherPriority”,因为它不是委托类型 [英] Cannot convert anonymous method to type 'System.Windows.Threading.DispatcherPriority' because it is not a delegate type
问题描述
有人能解释我要解决这个错误吗?
Can anyone explain what I need to do to get past this error?
无法将匿名方法转换为类型'System.Windows.Threading.DispatcherPriority',因为它不是委托类型
"Cannot convert anonymous method to type 'System.Windows.Threading.DispatcherPriority' because it is not a delegate type"
private void Test(object sender)
{
base.Dispatcher.BeginInvoke(delegate
{
//some code
}, new object[0]);
}
谢谢
推荐答案
如果使用的是.NET 3.5 SP1及更高版本,则可以添加对 System.Windows.Presentation.dll 的引用,并确保您拥有使用System.Windows.Threading;
在文件顶部。它包含易于使用的扩展方法,并允许您简单地编写:
If you're using .NET 3.5 SP1 and upwards, then you can add a reference to System.Windows.Presentation.dll and make sure you have using System.Windows.Threading;
at the top of the file. It contains extension methods that are easier to use, and allow you to simply write:
base.Dispatcher.BeginInvoke(() => { /* some code */ });
如果您使用的是不带SP1或更低版本的.NET 3.5,则必须将委托到具体的委托类型:
If you're using .NET 3.5 without SP1 or lower, then you'll have to cast the delegate to a concrete delegate type:
base.Dispatcher.BeginInvoke((Action) delegate { /* some code */ }, new object[0]);
这篇关于无法将匿名方法转换为类型“ System.Windows.Threading.DispatcherPriority”,因为它不是委托类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!