Mic*_*cah 72 c# wpf lambda dispatcher begininvoke
我正试着打电话System.Windows.Threading.Dispatcher.BeginInvoke.方法的签名是这样的:
BeginInvoke(Delegate method, params object[] args)
Run Code Online (Sandbox Code Playgroud)
我试图传递一个Lambda而不是创建一个Delegate.
_dispatcher.BeginInvoke((sender) => { DoSomething(); }, new object[] { this } );
Run Code Online (Sandbox Code Playgroud)
它给了我一个编译错误说我
无法将lambda转换为System.Delegate.
委托的签名将对象作为参数并返回void.我的lambda匹配这个,但它不起作用.我错过了什么?
Ree*_*sey 68
由于该方法采用System.Delegate,因此需要为其指定一种特定类型的委托,如此声明.这可以通过强制转换或通过新的DelegateType创建指定的委托来完成,如下所示:
_dispatcher.BeginInvoke(
new Action<MyClass>((sender) => { DoSomething(); }),
new object[] { this }
);
Run Code Online (Sandbox Code Playgroud)
另外,正如SLaks所指出的,Dispatcher.BeginInvoke采用了一个params数组,所以你可以写:
_dispatcher.BeginInvoke(
new Action<MyClass>((sender) => { DoSomething(); }),
this
);
Run Code Online (Sandbox Code Playgroud)
或者,如果DoSomething是此对象本身的方法:
_dispatcher.BeginInvoke(new Action(this.DoSomething));
Run Code Online (Sandbox Code Playgroud)
Erw*_*yer 67
短:
_dispatcher.BeginInvoke((Action)(() => DoSomething()));
Run Code Online (Sandbox Code Playgroud)
如果您从项目中引用System.Windows.Presentation.dll并添加,using System.Windows.Threading则可以访问允许您使用lambda语法的扩展方法.
using System.Windows.Threading;
...
Dispatcher.BeginInvoke(() =>
{
});
Run Code Online (Sandbox Code Playgroud)
使用Inline Lambda ...
Dispatcher.BeginInvoke((Action)(()=>{
//Write Code Here
}));
Run Code Online (Sandbox Code Playgroud)
我们为此创建扩展方法。例如
public static void BeginInvoke(this Control control, Action action)
=> control.BeginInvoke(action);
Run Code Online (Sandbox Code Playgroud)
现在我们可以从表单中调用它this.BeginInvoke(() => { ... }):