I am making some custom ICommand implementation of my own and I see A LOT of implementations going like this:
public event EventHandler CanExecuteChanged
{
a
This is an answer to this answer. It is true that the CanExecuteChanged?.Invoke(this, null);
has to be called by the main UI thread.
Simply write it like follows:
public void RaiseCanExecuteChanged()
{
Application.Current.Dispatcher.Invoke(() => CanExecuteChanged?.Invoke(this, null));
}
This solves your problem and you can requery just one command. It is however true that you should nevertheless make your CanExecute
-Method as fast as possible, as it will anyways be periodically executed.
It is best to have CanExecute just consist of a single return foo;
where foo
is a field you can set right before you call CommandManager.InvalidateRequerySuggested();
.