Cam*_*and 2 system.reactive reactiveui
我正在使用ReactiveUI和提供的ReactiveCollection<>类.
在ViewModel中我有一组对象,我希望创建一个observable来监视这些项目的IsValid属性.
这是我想要解决的方案.在我的ViewModel的构造函数中.
this.Items = new ReactiveCollection<object>();
IObservable<bool> someObservable = // ... how do I watch Items so when
// any items IsValid property changes,
// this observable changes. There
// is an IValidItem interface.
this.TheCommand = new ReactiveCommand(someObservable);
...
interface IValidItem { bool IsValid { get; } }
Run Code Online (Sandbox Code Playgroud)
编辑保罗的回答让我大部分都在那里.解决方案如下.
this.Items = new ReactiveCollection<object>();
this.Items.ChangeTrackingEnabled = true;
var someObservable = this.Items.Changed
.Select(_ => this.Items.All(i => i.IsValid));
Run Code Online (Sandbox Code Playgroud)
这取决于你想对IsValid的结果做什么.这是我将如何做到这一点,虽然它并不完全直观:
// Create a derived collection which are all the IsValid properties. We don't
// really care which ones are valid, rather that they're *all* valid
var isValidList = allOfTheItems.CreateDerivedCollection(x => x.IsValid);
// Whenever the collection changes in any way, check the array to see if all of
// the items are valid. We could probably do this more efficiently but it gets
// Tricky™
IObservable<bool> areAllItemsValid = isValidList.Changed.Select(_ => isValidList.All());
theCommand = new ReactiveCommand(areAllItemsValid);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2204 次 |
| 最近记录: |