RelayCommand in C #

I am learning MVVM to develop Silverlight C # from

http://msdn.microsoft.com/en-us/magazine/dd419663.aspx

and I'm confused about the RelayCommand class mentioned in context. The code:

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 } 

Why I really donโ€™t understand how _execute and _canExecute in this case. (I'm new to C # and not even sure what Action and Predicate is. I know they are delegates, but what is the difference between them and how they work?)
And also, in the program, I did not receive the line

  add { CommandManager.RequerySuggested += value; } remove { CommandManager.RequerySuggested -= value; } 

Can someone give me an explanation about this, thanks!

+6
source share
2 answers

A RelayCommand requires two pieces of information:

  • What code should be executed when the command is executed ( _execute action)
  • What code should be run to determine if this command can be executed (predicate _canExecute )

An Action is a delegate representing a method that returns void . In this case, the _execute action takes one parameter (a object ) and returns void .

A Predicate is a delegate that takes a value and returns a boolean result. In this case, the predicate _canExecute takes the value object and returns a bool .

Both _execute and _canExecute supplied to RelayCommand when they are built, because these are parts of the command that are unique to each individual command.

Regarding the CanExecuteChanged event:

 public event EventHandler CanExecuteChanged { add { CommandManager.RequerySuggested += value; } remove { CommandManager.RequerySuggested -= value; } } 

When a subscriber subscribes to an event, add is called and when they are unsubscribed, remove is called. The aforementioned CanExecuteChanged event is simply a CanExecuteChanged -through event (i.e., if the subscriber subscribes to the CanExecuteChanged event, he automatically subscribes to the CommandManager.RequerySuggested event). According to MSDN , the CommandManager.RequerySuggested ... event

occurs when the CommandManager detects conditions that may change the ability to execute a command.

I believe that the caller will most likely call the CanExecute method in RelayCommand when this event is fired to determine if the command can be executed.

+2
source

Adding / removing is similar to get / set property, but for events. The add code will be called when

 RelayCommandInstance.CanExecuteChanged += MyChangedEventHandler 
Performed

. The remove code is called when = = is executed. You do not see this syntax very strongly, since you rarely have to override the default behavior of adding / removing a delegate from the list of event calls.

In this case, RelayCommand simply maps the CanExecuteChanged event to the CommandManager.RequerySposed event.

+1
source

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


All Articles