使用我读约什 - 史密斯” WPF Apps With The Model-View-ViewModel Design Pattern教程需要帮助理解MVVM教程,RelayCommand&lambda表达式
我不明白下面的代码试图做的事。
首先,该语法提醒我属性,但用添加/删除来代替。
但什么是CommandManager.RequerySuggested
?
它代表事件订阅 的CommandManager.RequerySuggested 事件。这确保了WPF 指挥基础设施要求所有 RelayCommand对象,如果他们能 执行时,它会询问内置 命令
//Figure 3 The RelayCommand Class
public class RelayCommand : ICommand
{
#region Fields
readonly Action<object> _execute;
readonly Predicate<object> _canExecute;
#endregion // Fields
#region Constructors
public RelayCommand(Action<object> execute) : this(execute, null)
{ }
public RelayCommand(Action<object> execute, Predicate<object> canExecute)
{
if (execute == null) throw new ArgumentNullException("execute");
_execute = execute;
_canExecute = canExecute;
}
#endregion // Constructors
#region ICommand Members
[DebuggerStepThrough]
public bool CanExecute(object parameter)
{
return _canExecute == null ? true : _canExecute(parameter);
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public void Execute(object parameter)
{ _execute(parameter); }
#endregion // ICommand Members }
另外,保存命令配置了lambda表达式。第一,有2个参数变量。 他们会冲突吗?我不能只是像RelayCommand(this.Save(), this.CanSave)
那样做,或者是没有这样的语法。
_saveCommand = new RelayCommand(param => this.Save(),
param => this.CanSave);
这可以帮助你。 http://stackoverflow.com/questions/2281566/is-josh-smiths-implementation-of-the-relaycommand-flawed – 2010-08-25 09:41:06