Wou*_*ter 5 c# mvvm system.reactive reactiveui
我在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));
Run Code Online (Sandbox Code Playgroud)
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);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
634 次 |
| 最近记录: |