【问题标题】:How to disable a button during ReactiveCommand execution, then re-enable after finished?如何在 ReactiveCommand 执行期间禁用按钮,然后在完成后重新启用?
【发布时间】:2017-11-09 21:32:09
【问题描述】:

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

当前代码正在运行,但我在这里依赖一个布尔标志: https://github.com/oscarvarto/RxCounter/blob/master/RxCounter/ClickCounterViewModel.cs#L13-L18 通知按钮ViewModel.IOIntensiveCmd 完成执行的时刻。

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

例如,我如何生成一个IObservable<boolean> 来切换由 ReactiveCommand 的开始/结束发出的 Button.Enabled 标志?

【问题讨论】:

  • ReactiveCommand 有一个 IObservable IsExecuting。所以你可以简单地做 YourCommand.IsExecuting.ToProperty(this, vm => vm.IsBusy, out _isBusy)

标签: c# android xamarin xamarin.android reactiveui


【解决方案1】:

ReactiveUI 有几个帮助器:

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

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

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

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

【讨论】:

猜你喜欢
  • 2014-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-15
  • 2012-01-15
  • 2022-07-19
  • 1970-01-01
相关资源
最近更新 更多