如何将 ReactiveCommand 与 IObservable 绑定?

How to bind ReactiveCommand with IObservable?

我有一个命令 (ReactiveCommand),我想在用户选择列表中的项目时执行这个命令。

列表公开了一个可观察对象 IObservable<object> 所以每次有一个新事件发送到这个可观察对象时我都想执行我的命令。

我想到了这个,但它的功能似乎很复杂。

source.ElementSelected
    .Cast<Item>()
    .SelectMany(ViewModel.ShowDetailsCommand.Execute)
    .Subscribe();

不知道有没有更好的方法呢?命令和控件是否存在类似 BindCommand 的内容?

通常我会拥有你拥有的,但使用 WhenAnyObservable 或类似的。 RxUI 中的大多数示例也使用类似的语法将一个可观察对象链接到一个执行对象中。

虽然 InvokeCommand 是一种稍微简化的方法。

https://reactiveui.net/docs/handbook/commands/invoking-commands

否则可能只需要制作您自己的扩展方法。

此外,我只想确保指出

InvokeCommand respects the command's executability. That is, if the command's CanExecute method returns false, InvokeCommand will not execute the command when the source observable ticks.