如何在 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
思路如下:
- 在执行长 运行
Task
(或 Task<T>
)期间应禁用按钮。
- 点击应该在 ViewModel 中触发 ReactiveCommand
- ReactiveCommand 应该执行长 运行
Task
(或 Task<T>
)。
参见 https://github.com/oscarvarto/RxCounter/blob/master/RxCounter/ClickCounterViewModel.cs#L22
Task
(或Task<T>
)按钮完成后应重新启用。
当前代码有效,但我在这里依赖布尔标志:
https://github.com/oscarvarto/RxCounter/blob/master/RxCounter/ClickCounterViewModel.cs#L13-L18
在 ViewModel.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 可观察对象可能对按钮文本有帮助。
我编写了一个使用 ReactiveUI 的简单 Xamarin.Android 应用程序: https://github.com/oscarvarto/RxCounter/blob/master/RxCounter/MainActivity.cs 思路如下:
- 在执行长 运行
Task
(或Task<T>
)期间应禁用按钮。 - 点击应该在 ViewModel 中触发 ReactiveCommand
- ReactiveCommand 应该执行长 运行
Task
(或Task<T>
)。 参见 https://github.com/oscarvarto/RxCounter/blob/master/RxCounter/ClickCounterViewModel.cs#L22 Task
(或Task<T>
)按钮完成后应重新启用。
当前代码有效,但我在这里依赖布尔标志:
https://github.com/oscarvarto/RxCounter/blob/master/RxCounter/ClickCounterViewModel.cs#L13-L18
在 ViewModel.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 可观察对象可能对按钮文本有帮助。