我有一个类似 ReactiveUI 的视图模型。它具有多种不同类型的属性,可以激发NotifyPropertyChanged
事件,我想订阅一个在触发任何事件时将被调用的方法,但我对实际值不感兴趣。
我当前的代码有点难看(由于不透明true
选择)。有没有一种方式可以表达出在事件发生时只是关心的意图?
this.ObservableForProperty(m => m.PropertyOne)
.Select(_ => true)
.Merge(this.ObservableForProperty(m => m.PropertyTwo).Select(_ => true))
.Subscribe(...)
我正在合并大约 8 个属性,因此它比显示的更难看。
由于这看起来像 ReactiveUI,那么使用 WhenAny 运算符怎么样:
this.WhenAny(x => x.PropertyOne, x => x.PropertyTwo, (p1, p2) => Unit.Default)
.Subscribe(x => /* ... */);
但一般来说,如果您组合任意 Observables,您也可以使用非扩展方法更清楚地编写:
Observable.Merge(
this.ObservableForProperty(x => x.PropertyOne).Select(_ => Unit.Default),
this.ObservableForProperty(x => x.PropertyTwo).Select(_ => Unit.Default),
this.ObservableForProperty(x => x.PropertyThree).Select(_ => Unit.Default)
).Subscribe(x => /* ... */);
另外,如果您要订阅 ReactiveObject 的每个属性,最好只使用:
this.Changed.Subscribe(x => /* ... */);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)