InvokeCommand arguments with ReactiveUI 7
Asked Answered
L

1

8

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);
Lesleylesli answered 19/12, 2016 at 19:44 Comment(3)
Did you figure out what was causing the problem? I have the same problem.Ibsen
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<TReturnValue,Unit>. 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);Lesleylesli
That was obvious... thanks. Oh, also, you should post an answer to your own question and accept it, rather than commenting ;)Whitlow
L
2

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);
Lesleylesli answered 1/3, 2019 at 1:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.