Mvvm 如何在ReactiveUI中订阅可观察集合中的嵌套属性

Mvvm 如何在ReactiveUI中订阅可观察集合中的嵌套属性,mvvm,system.reactive,reactiveui,Mvvm,System.reactive,Reactiveui,如何订阅可观察集合中的嵌套属性 视图模型: Public Class ViewModel{ //a collection of all Hardware Tests. public observableCollection<HWTest> AllHWTests; //constructors ViewModel{ //here I want to subscribe to Ispassed changes. }

如何订阅可观察集合中的嵌套属性

视图模型:

Public Class ViewModel{

    //a collection of all Hardware Tests.
    public observableCollection<HWTest> AllHWTests;


    //constructors
    ViewModel{

    //here I want to subscribe to Ispassed changes.
    
    }



}



我如何订阅iPassed

这只是一个例子,我当时没有写任何代码

提前感谢。

开箱即用的ObservableCollection不会提供您想要的。它的名字表明您可以观察列表内部的事物列表上元素的INotifyPropertyChangedEvent

通常,我们希望了解给定ObservableCollection的INotifyCollectionChanged和INotifyPropertyChanged,这需要一些手动设置。我就这个话题写了一篇文章

TLDR

你想用的是。它将允许您从列表的内部元素获取属性更改通知。

ObservableCollection仅允许您观察集合成员的更改。您需要使用它来附加到每个类成员实现的事件,以查看集合成员中的各个属性何时发生更改

下面是如何使用1个ObservableCollection和2个Iobbservable来完成您的要求

首先,我们需要设置我们想要观察的课程:

public class HWInterface : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    
    public HWInterface(string name)
    {
        this.Name = name;
    }
    
    public string Name { get; private set; }
    
    private bool _isPassed = false;
    public bool IsPassed
    {
        get => _isPassed;
        set
        {
            if (_isPassed != value)
            {
                _isPassed = value;
                this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("IsPassed"));
            }
        }
    }
}
请注意,我已将您的大小写更改为使用普通的C惯用形式

现在我需要一个可观察的集合来处理:

var oc = new ObservableCollection<HWInterface>();
以下是我的输出:

1: B "IsPassed" changed to True.
2: B "IsPassed" changed to True.
1: B "IsPassed" changed to False.
2: B "IsPassed" changed to False.
1: C "IsPassed" changed to True.
2: C "IsPassed" changed to True.

请为当前的一个编写更完整的代码。我不理解您的问题。谢谢,我编辑了代码,我不知道如何订阅viewmodel类中的isPassed属性。@BarakD-您是指ObservableCollection还是IObservable?您无法订阅ObservableCollection。我是reactiveui的新手,因此我使用ObservableCollection来观察视图模型@BarakD-中的所有HWTest,但这并没有回答我的问题。你是说可观察的收集还是可观察的?您无法订阅ObservableCollection。
var oc = new ObservableCollection<HWInterface>();
PropertyChangedEventHandler pceh = (s, e)  => Console.WriteLine($"1: {(s as HWInterface).Name} \"{e.PropertyName}\" changed to {(s as HWInterface).IsPassed}.");

oc.CollectionChanged += (s, e) =>
{
    if (e.NewItems != null) foreach (var f in e.NewItems.OfType<HWInterface>()) f.PropertyChanged += pceh;
    if (e.OldItems != null) foreach (var f in e.OldItems.OfType<HWInterface>()) f.PropertyChanged -= pceh;
};
var query =
    Observable
        .FromEventPattern<NotifyCollectionChangedEventHandler, NotifyCollectionChangedEventArgs>(
            h => oc.CollectionChanged += h,
            h => oc.CollectionChanged -= h)
        .Select(x =>
            oc
                .ToObservable()
                .SelectMany(f =>
                    Observable
                        .FromEventPattern<PropertyChangedEventHandler, PropertyChangedEventArgs>(
                            h => f.PropertyChanged += h,
                            h => f.PropertyChanged -= h)
                        .Select(y => new { foo = f, property = y.EventArgs.PropertyName })))
        .Switch();
        
IDisposable subscription =
    query
        .Subscribe(x => Console.WriteLine($"2: {x.foo.Name} \"{x.property}\" changed to {x.foo.IsPassed}."));
var a = new HWInterface("A");
var b = new HWInterface("B");
var c = new HWInterface("C");

oc.Add(a);
oc.Add(b);
oc.Add(c);

b.IsPassed = true;

b.IsPassed = false;

oc.Remove(b);

b.IsPassed = true; // `b` has been removed so doesn't fire anymore
c.IsPassed = true;
1: B "IsPassed" changed to True.
2: B "IsPassed" changed to True.
1: B "IsPassed" changed to False.
2: B "IsPassed" changed to False.
1: C "IsPassed" changed to True.
2: C "IsPassed" changed to True.