使用我讀約什 - 史密斯」 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