我们如何从IObservable <IList <T >>创建IObservableList <T>? 问题上下文我尝试过的

问题描述

问题

我们如何从DynamicDataIObservable<IReadOnlyList<T>>IObservableList<T>

上下文

我在项目中同时使用Reactive extensionsDynamicData

我目前有一个随时间变化的字符串列表。我身为IObservable<IReadOnlyList<string>>。它产生如下更新:

  1. ["A","C","F"]
  2. ["A","B","C"]

我想将其转换为IObservableList<string>,这样会产生如下更新:

  1. 在索引0处添加“ A”,“ C”,“ F”
  2. 在索引1处添加“ B”
  3. 从索引3中删除“ F”

我尝试过的

我尝试使用ToObservableChangeSet扩展名,但是对于我的情况,它的行为不正确。

Subject<IReadOnlyList<string>> source = new Subject<IReadOnlyList<string>>();
IObservable<IEnumerable<string>> observableOfList = source;
IObservable<IChangeSet<string>> observableOfChangeSet = source.ToObservableChangeSet<string>();

observableOfChangeSet
    .Bind(out ReadOnlyObservableCollection<string> boundCollection)
    .Subscribe();

((INotifyCollectionChanged)boundCollection).CollectionChanged += OnCollectionChanged;

source.OnNext(new[] { "A","F" });
source.OnNext(new[] { "A","C" });

void OnCollectionChanged(object sender,NotifyCollectionChangedEventArgs e)
{
    Debug.WriteLine($"[{string.Join(",",(IEnumerable<string>)sender)}] (operation: {e.Action} at index {e.NewStartingIndex})");
}
输出量
[A,C,F] (operation: Reset at index -1)
[A,F,A] (operation: Add at index 3)
[A,A,B] (operation: Add at index 4)
[A,B,C] (operation: Add at index 5)

我们可以看到[A,C][A,C]不同。


我也尝试过使用EditDiff,但这并不能保留列表的顺序。

Subject<IReadOnlyList<string>> source = new Subject<IReadOnlyList<string>>();
IObservable<IEnumerable<string>> observableOfList = source;
IObservable<IChangeSet<string>> observableOfChangeSet = ObservableChangeSet.Create<string>(list =>
{
    return observableOfList
        .Subscribe(items => list.EditDiff(items,EqualityComparer<string>.Default));
});

observableOfChangeSet
    .Bind(out ReadOnlyObservableCollection<string> boundCollection)
    .Subscribe();

((INotifyCollectionChanged)boundCollection).CollectionChanged += OnCollectionChanged;

source.OnNext(new[] { "A",C] (operation: Remove at index -1)
[A,B] (operation: Add at index 2)

我们可以看到[A,B][A,C]不同。

谢谢!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)