Question

When defining a RelayCommand in the ViewModel, this is normally done using once [lazy or in constructor] (see here for examples).

But is this safe ? What if multiple RelayCommands from the same instance are queued (multiple clicks from same button on the GUI), and for the first command the 'CanExecute' is changed to false, will the other queued commands also be cancelled ? I can imagine that this not the correct behavior ?

Was it helpful?

Solution

Multiple commands won't be queued. The RelayCommand is data-bound so it will be executed on the UI thread. There is only one UI thread, so one instance of the RelayCommand would have to finish execution before the next click entered. If you have long-running operations, you'll typically disable the command and kick of an asynchronous operation or spawn a background job, and then when the UI is released, the command will already be disabled before the next click is processed.

OTHER TIPS

Your execute action should double check whether the command can execute or not. CanExecute is a hint for anything binding to the command, but your ViewModel shouldn't make any assumptions about how the execute action is being called.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top