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:
在Silverlight 4中,我有一个自定义服务类,它具有异步的Completed事件。在Completed事件中,我获取返回的数据并通过以下方式调用populate方法:
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:
BeginInvoke在SL4中工作,但是当我将它移植到WPF时,我收到以下错误:
Cannot convert lambda expression to type 'System.Delegate' because it is not a delegate type
无法将lambda表达式转换为类型'System.Delegate',因为它不是委托类型
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.
知道如何在WPF中使用它吗?重构使用BackgroundWorker对我来说不是一个选择。
2 个解决方案
#1
26
You need to specify an explicit delegate type. Just use an Action
.
您需要指定显式委托类型。只需使用动作即可。
Dispatcher.BeginInvoke(new Action(() => populateInbox(args.Jobs));
You could, however, avoid having to close over the args.Jobs
value like this:
但是,您可以避免必须关闭args.Jobs值,如下所示:
Dispatcher.BeginInvoke(new Action((jobs) => populateInbox(jobs)), jobs);
This is because the single-parameter version of Dispatcher.BeginInvoke
has a different signature in Silverlight than in WPF. In Silverlight, it takes an Action
, which allows the C# compiler to implicitly type your lambda as an Action
. In WPF, it takes a Delegate
(like its Control.BeginInvoke
analog in Winforms), so the C# compiler has to have a delegate type explicitly specified.
这是因为Dispatcher.BeginInvoke的单参数版本在Silverlight中具有与WPF中不同的签名。在Silverlight中,它采用一个Action,它允许C#编译器隐式地将lambda键入为Action。在WPF中,它需要一个Delegate(就像它在Winforms中的Control.BeginInvoke模拟一样),因此C#编译器必须明确指定一个委托类型。
#2
2
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.
在WPF和winforms中,您必须首先将它转换为MethodInvoker,否则您将收到错误无法将匿名方法转换为类型'System.Delegate',因为它不是委托类型。
Dispatcher.BeginInvoke((MethodInvoker) delegate(List<JobViewModel> jobs)
{
inbox.DataContext = jobs;
});
For more info: http://msdn.microsoft.com/en-us/library/system.windows.forms.methodinvoker.aspx
有关详细信息:http://msdn.microsoft.com/en-us/library/system.windows.forms.methodinvoker.aspx
#1
26
You need to specify an explicit delegate type. Just use an Action
.
您需要指定显式委托类型。只需使用动作即可。
Dispatcher.BeginInvoke(new Action(() => populateInbox(args.Jobs));
You could, however, avoid having to close over the args.Jobs
value like this:
但是,您可以避免必须关闭args.Jobs值,如下所示:
Dispatcher.BeginInvoke(new Action((jobs) => populateInbox(jobs)), jobs);
This is because the single-parameter version of Dispatcher.BeginInvoke
has a different signature in Silverlight than in WPF. In Silverlight, it takes an Action
, which allows the C# compiler to implicitly type your lambda as an Action
. In WPF, it takes a Delegate
(like its Control.BeginInvoke
analog in Winforms), so the C# compiler has to have a delegate type explicitly specified.
这是因为Dispatcher.BeginInvoke的单参数版本在Silverlight中具有与WPF中不同的签名。在Silverlight中,它采用一个Action,它允许C#编译器隐式地将lambda键入为Action。在WPF中,它需要一个Delegate(就像它在Winforms中的Control.BeginInvoke模拟一样),因此C#编译器必须明确指定一个委托类型。
#2
2
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.
在WPF和winforms中,您必须首先将它转换为MethodInvoker,否则您将收到错误无法将匿名方法转换为类型'System.Delegate',因为它不是委托类型。
Dispatcher.BeginInvoke((MethodInvoker) delegate(List<JobViewModel> jobs)
{
inbox.DataContext = jobs;
});
For more info: http://msdn.microsoft.com/en-us/library/system.windows.forms.methodinvoker.aspx
有关详细信息:http://msdn.microsoft.com/en-us/library/system.windows.forms.methodinvoker.aspx