Need help understanding MVVM tutorial, RelayCommand & Use lambdas

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.

//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 }

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 );
+3
1
  • CommandManager.RequerySuggested += value , CanExecute true, false .

    WPF Button/MenuItem (CommandButtonBase), false , true.
    , WPF ( Button/MenuItem , .

  • (-) Action<object> a Predicate<object> . , , , (params - ), - ).

    ,

    • private void Save(object obj)

      private bool CanSave(object obj)

    , () - RelayCommand(this.Save,this.CanSave) .

+3

Source: https://habr.com/ru/post/1760996/


All Articles