问题描述
问题
我们如何从DynamicData到IObservable<IReadOnlyList<T>>
到IObservableList<T>
?
上下文
我在项目中同时使用Reactive extensions和DynamicData。
我目前有一个随时间变化的字符串列表。我身为IObservable<IReadOnlyList<string>>
。它产生如下更新:
-
["A","C","F"]
-
["A","B","C"]
我想将其转换为IObservableList<string>
,这样会产生如下更新:
- 在索引0处添加“ A”,“ C”,“ F”
- 在索引1处添加“ B”
- 从索引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 (将#修改为@)