InvokeCommand arguments with ReactiveUI 7

2.2k views Asked by At

I'm switching to the latest version of ReactiveUI (7.0) and I'm running into some incompatibilities and would like to know the suggested way to handle this:

ReactiveUI 6.x

Texts.Events().MouseUp
     .InvokeCommand(ViewModel, x => x.DoSomething);

This now throws an exception:

Command requires parameters of type System.Reactive.Unit, but received parameter of type System.Windows.Input.MouseButtonEventArgs.

I fixed this by using the following code, but is this the right way?

Texts.Events().MouseUp
     .Select(x => Unit.Default)
     .InvokeCommand(ViewModel, x => x.DoSomething);
1

There are 1 answers

0
Niek Jannink On BEST ANSWER

The argument that the command was expection was Unit, meaning a command without an input argument which in the case of ReactiveUI is a ReactiveCommand. Thats why in the example above you have to 'convert' the MouseButtonEventArgs from the event to a Unit. For this I created a helper extension method ToSignal:

public static IObservable<Unit> ToSignal<TDontCare>(this IObservable<TDontCare> source) 
    => source.Select(_ => Unit.Default);

\\ The subscription will be then
Texts.Events().MouseUp
     .ToSignal()
     .InvokeCommand(ViewModel, x => x.DoSomething);