I am reading Josh Smith WPF Application with Model-View-ViewModel design pattern
I do not understand what the following code is trying to do.
First, the syntax reminds me of properties, but it adds / removes instead.
But what is it CommandManager.RequerySuggested?
He delegates a subscription to the CommandManager.RequerySposed event. This ensures that the WPF command infrastructure requests all RelayCommand objects if they can execute when it requests the built-in Commands.
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 }
In addition, the save command is configured using lambdas. 1, there are 2 variable parameters. Will they conflict? I can't just do something like RelayCommand(this.Save(), this.CanSave)or not such syntax.
_saveCommand = new RelayCommand(param => this.Save(),
param => this.CanSave );