2016-12-01 56 views
-1

我试图将方法作为参数传递。该方法不返回任何东西(void),因此我以Action的形式执行。当我从命令中调用Execute时会发生错误。这是错误:函数作为参数(无返回结果类型)

Can not be converted from ' void ' to ' System.Action '.

任何帮助吗?

private async void Execute(Action runAction) 
{ 
    ... 

    await TaskEx.Run(() => runAction); 

    ... 

} 

_command1 = new RelayCommand(Execute(Class.ExecuteVoidMethod1()),() => CanExecuteVoidMethod1()); 

编辑

_command1 = new RelayCommand(Execute(() => Class.ExecuteVoidMethod1()),() => CanExecuteVoidMethod1()); // Same Error 

_command1 = new RelayCommand(Execute(() => Class.ExecuteVoidMethod1),() => CanExecuteVoidMethod1()); // Only assignment, call, increment, decrement, await expresion and new object expressions can be used as a statement 
+0

@ThePerplexedOne嗨,谢谢,但是当你需要返回值时使用Func?这不是这样吗? – avechuche

+0

[参数类型'void'可能重复不能分配给参数类型'System.Action'](http://stackoverflow.com/questions/3387812/argument-type-void-is-not-assignable-to-parameter -type-system-action) –

+3

你需要使用'TaskEx.Run(()=> runAction())或者只是'TaskEx.Run(runAction)'。目前你正在传递一个'Func '。 – Lee

回答

0

你为什么不打电话给你的Execute方法操作?

await TaskEx.Run(() => runAction); 

你必须像这样运行:

await TaskEx.Run(() => runAction()); 

在其他情况下,你是不是返回运行它的你的行动。

相关问题