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.
source share