-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
@ThePerplexedOne嗨,谢谢,但是当你需要返回值时使用Func?这不是这样吗? – avechuche
[参数类型'void'可能重复不能分配给参数类型'System.Action'](http://stackoverflow.com/questions/3387812/argument-type-void-is-not-assignable-to-parameter -type-system-action) –
你需要使用'TaskEx.Run(()=> runAction())或者只是'TaskEx.Run(runAction)'。目前你正在传递一个'Func'。 –
Lee