如何在 ReactiveCommand 执行期间禁用按钮,然后在完成后重新启用?

How to disable a button during ReactiveCommand execution, then re-enable after finished?

我编写了一个使用 ReactiveUI 的简单 Xamarin.Android 应用程序: https://github.com/oscarvarto/RxCounter/blob/master/RxCounter/MainActivity.cs 思路如下:

当前代码有效,但我在这里依赖布尔标志: https://github.com/oscarvarto/RxCounter/blob/master/RxCounter/ClickCounterViewModel.cs#L13-L18ViewModel.IOIntensiveCmd 完成执行时通知按钮。

我如何使用 Observables 或 ReactiveUI 机制在 ReactiveCommand 执行期间禁用然后启用按钮?

例如,我如何生成一个 IObservable<boolean> 来切换由 ReactiveCommand 的 start/end 发出信号的 Button.Enabled 标志?

ReactiveUI 有几个帮助程序:

直接在视图中将命令绑定到按钮(参见https://reactiveui.net/docs/handbook/commands/binding-commands

this.BindCommand(
    this.ViewModel,
    x => x.IOIntensiveCmd,
    x => x.MyButton);

使用 ReactiveCommand.IsExecuting(IObservable<bool>)。

例如,IsExecuting 可观察对象可能对按钮文本有帮助。