我正在爲每個Josh Smith應用MVVM模式並遇到困難。我一直在研究這個問題,似乎無法讓語法變得非常正確。RelayCommand lambda語法問題
下面的代碼在我看來像它遵循所需的語法,但Visual Studio報告錯誤「Delegate'System.Action'不在指定的行上使用'2'參數'。
有人可以看到我犯了什麼錯誤嗎?謝謝!
+湯姆
RelayCommand _relayCommand_MoveUp;
public ICommand RelayCommand_MoveUp
{
get
{
if (_relayCommand_MoveUp == null)
{
_relayCommand_MoveUp = new RelayCommand(
(sender, e) => this.Execute_MoveUp(sender, e), **ERROR REPORTED HERE**
(sender, e) => this.CanExecute_MoveUp(sender, e));
return _relayCommand_MoveUp;
}
}
}
private void Execute_MoveUp(object sender, ExecutedRoutedEventArgs e)
{
if (_selectedFolder != null)
{
_selectedFolder.SelectParent();
}
}
private void CanExecute_MoveUp(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = (_selectedFolder != null) && (_selectedFolder.Parent != null);
}
//And from Josh Smith:
public class RelayCommand : ICommand
{
public RelayCommand(Action<object> execute);
public RelayCommand(Action<object> execute, Predicate<object> canExecute);
public event EventHandler CanExecuteChanged;
[DebuggerStepThrough]
public bool CanExecute(object parameter);
public void Execute(object parameter);
}
謝謝,rmoore。那麼,我是否正確理解如果我需要訪問被調用方法中的EventArgs,我不能使用RelayCommand類來調用它? + tom – 2009-05-23 00:44:23
沒錯,基本的ICommand並沒有實現任何事件,事實上,據我所知,RoutedCommand實際上來自於一個叫做CommandBinding的東西,這就是RoutedCommand尋找的東西。 我在MVVM中爲我的文章添加了一些附加信息,因爲這裏沒有足夠的空間=) – rmoore 2009-05-23 04:13:33