I'm trying to convert my project from ReactiveUI 6.5 to version 7. In the old version I called
// var command = ReactiveCommand.Create...;
// ...
if(command.CanExecute(null))
command.Execute(null);
in order to execute a command from my code behind.
Now the CanExecute method is no longer available and replaced with a property of IObservable<bool>
. Is the CanExecute Observable automatically called if I just make a call to Execute().Subscribe()
or must I call it explicitly?
For now I replaced the above code with
command.Execute().Subscribe();
I found three different solutions to call my command's
CanExecute
andExecute
methods like I could before in ReactiveUI 6.5:Option 1
This is equal to the call in version 6.5, but we need to explicitly convert the command to an ICommand:
Option 2
or the async variant:
Option 3
Another option is to make us of the
InvokeCommand
extension method.This respects the command's executability, like mentioned in the documentation.
In order to make it more comfortable I've written a small extension method to provide a
ExecuteIfPossible
and aGetCanExecute
method:You can use this extension method as follows:
Note: You need the
Subscribe()
call at the end, just like you need it for the call toExecute()
, otherwise nothing will happen.Or if you want to use async and await:
If you want to check if a command can be executed, just call