C# 如何让ReactiveCommands观察自己的IsExecuting observable
我的ViewModel中有几个命令,我想让每个按钮的CanExecute绑定到一个可观察的busy,该busy定义为当前没有任何按钮正在执行 下面是我想到的,但显然它遇到了NullReferenceExceptionC# 如何让ReactiveCommands观察自己的IsExecuting observable,c#,mvvm,system.reactive,reactiveui,C#,Mvvm,System.reactive,Reactiveui,我的ViewModel中有几个命令,我想让每个按钮的CanExecute绑定到一个可观察的busy,该busy定义为当前没有任何按钮正在执行 下面是我想到的,但显然它遇到了NullReferenceException busy = Observable.CombineLatest(this.PlayCommand.IsExecuting, this.PauseCommand.IsExecuting, (play, pause) => play && pause); thi
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属性是只读的,因此我需要在初始化命令之前定义一个IObservable
有没有办法解决这个鸡和蛋的问题?如果能更好地观察ViewModel(或一组ViewModels)的繁忙状态,我们也会非常感激:-)我会通过使用主题设置代理:
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);
var areAllAvailable=新行为主体(true);
PauseCommand=新的反应命令(区域可用);
PlayCommand=新的反应命令(区域可用);
可观察。组合测试(PauseCommand.IsExecuting、PlayCommand.IsExecuting、,
(pa,pl)=>!(pa | pl))
.认购(所有可用);
很好,很好用。没想到这一点,我只会责怪‘科目不好’——到处流传的咒语:-)。另外,最后一行应该包括.Subscribe(n=>areAllAvailable.OnNext(n))
以更新主题。主题不一定是“坏的”,它们只是功能性不强。它们实际上非常适合将现有代码改编为Rx,因为这些代码可能也不起作用!