2016-12-01 55 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()); 

在其他情況下,你是不是返回運行它的你的行動。

相關問題