使用 ReactiveUI 7 调用命令参数

InvokeCommand arguments with ReactiveUI 7

我正在切换到最新版本的 ReactiveUI (7.0),但我 运行 遇到了一些不兼容问题,想知道处理此问题的建议方法:

ReactiveUI 6.x

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

现在抛出异常:

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

我使用以下代码解决了这个问题,但这是正确的方法吗?

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

预期命令的参数是 Unit,这意味着没有输入参数的命令,在 ReactiveUI 的情况下是 ReactiveCommand。这就是为什么在上面的示例中,您必须 'convert' 从事件到 Unit 的 MouseButtonEventArgs。为此,我创建了一个辅助扩展方法 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);