Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在ReactiveUI,C中动态地将一个reactivelist中的可观察对象合并为一个可观察对象#_C#_Reactiveui - Fatal编程技术网

C# 如何在ReactiveUI,C中动态地将一个reactivelist中的可观察对象合并为一个可观察对象#

C# 如何在ReactiveUI,C中动态地将一个reactivelist中的可观察对象合并为一个可观察对象#,c#,reactiveui,C#,Reactiveui,我试着用ReactiveUI来表达一些可观察的东西。虽然有些东西已经很好地工作了,但我仍然无法为其他东西找到合适的反应UI方法。我还是个新手 public class ViewModel : ReactiveObject { public ReactiveList<A> aList {get;} } public class A : ReactiveObject { public ReactiveList<B> bList {get;} } publi

我试着用ReactiveUI来表达一些可观察的东西。虽然有些东西已经很好地工作了,但我仍然无法为其他东西找到合适的反应UI方法。我还是个新手

public class ViewModel : ReactiveObject {
    public ReactiveList<A> aList {get;}
}

public class A : ReactiveObject {
    public ReactiveList<B> bList {get;}
}

public class B : ReactiveObject {
    //some properties
}
但是,当执行此代码时,这只会观察到
B
中已经存在的更改。是否有什么东西可以自动观察
aList
bList
中的更改,并订阅新项目

或者可以
可观察。合并
查看
反应列表
,并自动订阅任何新项目和取消订阅已删除的项目


我知道,我可以手动完成,但这可能不会像预期的那样使用ReactiveUI。有什么想法吗?

反应列表
上的
已更改
属性将在列表更改时通知您。使用此选项可了解何时根据包含的
ReactiveList
s中的更改更新订阅

要获得每次
ReactiveList
ReactiveList
更改时都会滴答作响的可观察值,可以执行以下操作:

aList.Changed
    .Select(change => Observable.Merge(viewModel.aList.Select(x => Observable.Merge(x.bList.Select(y => y.Changed)))) // Create observable to notify when the sub collections change
        .StartWith(change) // Send the notification that aList has changed immediately
        )
    .Switch() // Only subscribe to the latest change notifications from the sub collections in the most recent aList
    .Subscribe(changes => { /* Do something */});

您可以尝试使用我们的库,而不是使用ReactiveList,它允许您使用LINQ展平可观察的集合

public class ViewModel : ReactiveObject {
    public ICompositeSourceList<A> aList {get;}
}

public class A : ReactiveObject {
    public ICompositeSourceList<B> bList {get;}
}

public class B : ReactiveObject {
    //some properties
}
public类ViewModel:ReactiveObject{
公共ICompositeSourceList

public class ViewModel : ReactiveObject {
    public ICompositeSourceList<A> aList {get;}
}

public class A : ReactiveObject {
    public ICompositeSourceList<B> bList {get;}
}

public class B : ReactiveObject {
    //some properties
}
ViewModel vm = ... ;

IComposteList<B> all = 
    from aItem in vm.aList
    from bItem in aItem.bList
    select bItem;

// Subscribe to the stream of flattened items
all.Items.Subscribe((ImmutableList<B> bItems)=>DoSometing(bItems));

// or create an INPC object with a property Items
using(var s = allLines.Subscribe()){
   RenderLines(s.Items);
}
ObservableCollection<B> observableBs =
   all.CreateObservableCollection(EqualityComparer<B>.Default)