使用匿名委托的 Dispatcher.Invoke 在 Silverlight 中工作,但在 WPF 中不起作用
在 Silverlight 4 中,我有一个自定义服务类,它有一个异步 Completed 事件。在 Completed 事件中,我获取返回的数据并通过如下方式调用填充方法:
private void service_Completed(object sender, CompletedEventArgs args)
{
Dispatcher.BeginInvoke(() => populateInbox(args.Jobs));
}
private void populateInbox(List<JobViewModel> jobs)
{
inbox.DataContext = jobs;
}
BeginInvoke
在 SL4 中工作,但是当我将其移植到 WPF 时,出现以下错误:
无法将 lambda 表达式转换为“System.Delegate”类型,因为它不是委托类型
我尝试将其更改为内联、匿名、参数化委托:
Dispatcher.BeginInvoke(delegate(List<JobViewModel> jobs)
{
inbox.DataContext = jobs;
});
但是,这会产生相同的编译时错误。
知道如何让它在 WPF 中工作吗?重构以使用 BackgroundWorker
对我来说不是一个选择。
In Silverlight 4 I have a custom service class which has an asynchronous Completed event. Inside the Completed event I take the returned data and invoke a populate method via something like this:
private void service_Completed(object sender, CompletedEventArgs args)
{
Dispatcher.BeginInvoke(() => populateInbox(args.Jobs));
}
private void populateInbox(List<JobViewModel> jobs)
{
inbox.DataContext = jobs;
}
The BeginInvoke
works in SL4, however when I ported it to WPF I get the following error:
Cannot convert lambda expression to type 'System.Delegate' because it is not a delegate type
I tried changing it to an in-line, anonymous, paramaterized delegate:
Dispatcher.BeginInvoke(delegate(List<JobViewModel> jobs)
{
inbox.DataContext = jobs;
});
However, that yields the same compile-time error.
Any idea how to get this to work in WPF? Refactoring to use the BackgroundWorker
is not an option for me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要指定显式委托类型。只需使用
操作
。但是,您可以避免像这样关闭 args.Jobs 值:
这是因为单参数版本的 Dispatcher.BeginInvoke 在 Silverlight 中具有不同的签名比在 WPF 中。在 Silverlight 中,它需要一个
Action
,它允许 C# 编译器将 lambda 隐式键入为Action
。在 WPF 中,它需要一个Delegate
(就像 Winforms 中的Control.BeginInvoke
类似物),因此 C# 编译器必须显式指定一个委托类型。You need to specify an explicit delegate type. Just use an
Action
.You could, however, avoid having to close over the
args.Jobs
value like this:This is because the single-parameter version of
Dispatcher.BeginInvoke
has a different signature in Silverlight than in WPF. In Silverlight, it takes anAction
, which allows the C# compiler to implicitly type your lambda as anAction
. In WPF, it takes aDelegate
(like itsControl.BeginInvoke
analog in Winforms), so the C# compiler has to have a delegate type explicitly specified.在 WPF 和 winforms 中,您必须首先将其转换为 MethodInvoker,否则您将收到错误无法将匿名方法转换为类型“System.Delegate”,因为它不是委托类型。
有关详细信息:< a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.methodinvoker.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/库/system.windows.forms.methodinvoker.aspx
In WPF and winforms you must cast it to a MethodInvoker first, otherwise you will get the error Cannot convert anonymous method to type 'System.Delegate' because it is not a delegate type.
For more info: http://msdn.microsoft.com/en-us/library/system.windows.forms.methodinvoker.aspx