-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