如何让ReactiveCommands观察自己的IsExecuting可观察性

本文关键字:自己的 IsExecuting 可观察性 观察 ReactiveCommands | 更新日期: 2023-09-27 18:00:27

我的ViewModel中有几个命令,我希望将每个按钮的CanExecute绑定到一个可观察到的繁忙,该繁忙被定义为当前没有任何按钮在执行。

以下是我想到的,但很明显它遇到了一个NullReferenceException。

busy = Observable.CombineLatest(this.PlayCommand.IsExecuting, this.PauseCommand.IsExecuting, (play, pause) => play && pause);
this.PauseCommand = new ReactiveCommand(busy.Select(b => !b));
this.PlayCommand = new ReactiveCommand(busy.Select(b=> !b));

此外,ReactiveCommand的CanExecuteObservable属性是只读的,因此在初始化命令之前,我需要定义一个IOobservable。

关于如何解决鸡和蛋的问题有什么想法吗?观察ViewModel(或ViewModel集合)繁忙状态的更好方法也将受到赞赏:-)

如何让ReactiveCommands观察自己的IsExecuting可观察性

我会使用Subject:来设置代理

var areAllAvailable = new BehaviorSubject<bool>(true);
PauseCommand = new ReactiveCommand(areAllAvailable);
PlayCommand = new ReactiveCommand(areAllAvailable);
Observable.CombineLatest(PauseCommand.IsExecuting, PlayCommand.IsExecuting, 
    (pa,pl) => !(pa || pl))
    .Subscribe(allAreAvailable);