gpt4 book ai didi

system.reactive - 如何观察一组项目何时都有效?

转载 作者:行者123 更新时间:2023-12-01 14:38:39 24 4
gpt4 key购买 nike

我正在使用 ReactiveUI 和提供的 ReactiveCollection<>类。

在 ViewModel 中,我有一个对象集合,我希望创建一个可观察对象来监视这些项目的 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; } }

编辑 Ana 的回答让我明白了大部分内容。解决方案如下。

this.Items = new ReactiveCollection<object>();
this.Items.ChangeTrackingEnabled = true;

var someObservable = this.Items.Changed
.Select(_ => this.Items.All(i => i.IsValid));

最佳答案

这取决于您想对 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);

关于system.reactive - 如何观察一组项目何时都有效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11220880/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com