Search code examples
c#.netwinformsreactiveuicommand-pattern

How to implement command pattern in the newest versions of ReactiveUI?


I'm following the sample that given from the official GitHub page about using ReactiveUI.ReactiveCommand below. (ReactiveUI v8.0.1) github link

public class MainViewModel : ReactiveObject
{
    public ReactiveCommand ParameterlessCommand { get; }
    public MainViewModel()
    {
        ParameterlessCommand = ReactiveCommand.Create(Parameterless);
    }


    private void Parameterless()
    {
        
    }
}

The errors occur when I use this implementation with the ReactiveUI v13.0.38 enter image description here

  1. The first error is "CS0722 'ReactiveCommand': static types cannot be used as return types".
  2. The second error is "CS0029 Cannot implicitly convert type 'ReactiveUI.ReactiveCommand<System.Reactive.Unit, System.Reactive.Unit>' to 'ReactiveUI.ReactiveCommand'"

How can I use the command pattern in this MVVM code sample with ReactiveUI v13.0.38?


Solution

  • ReactiveCommand class which implements ICommand interface is a generic type.

    // Unit is used for void - no input and output, so double Unit
    public ReactiveCommand<Unit, Unit> ParameterlessCommand { get; }
    
    // assigning
    ParameterlessCommand = ReactiveCommand.Create(Parameterless);