C# 如何创建IObservableList<;T>;从IObservable<;IList<;T>>;? 问题:

C# 如何创建IObservableList<;T>;从IObservable<;IList<;T>>;? 问题:,c#,.net,system.reactive,reactive,reactiveui,C#,.net,System.reactive,Reactive,Reactiveui,我们如何从IObservable到IObservable列表(from) 上下文 我在我的项目中同时使用和 我目前有一个随时间变化的字符串列表。我将at作为一个IObservable。它会产生如下更新: [“A”、“C”、“F”] [“A”、“B”、“C”、“F”] [“A”、“B”、“C”] 我想将其转换为一个IObservableList,该列表将产生如下更新: 在索引0处添加“A”、“C”、“F” 在索引1处添加“B” 从索引3中删除“F” 我试过的 我尝试过使用ToObservableC

我们如何从
IObservable
IObservable列表
(from)

上下文 我在我的项目中同时使用和

我目前有一个随时间变化的字符串列表。我将at作为一个
IObservable
。它会产生如下更新:

  • [“A”、“C”、“F”]
  • [“A”、“B”、“C”、“F”]
  • [“A”、“B”、“C”]
  • 我想将其转换为一个
    IObservableList
    ,该列表将产生如下更新:

  • 在索引0处添加“A”、“C”、“F”
  • 在索引1处添加“B”
  • 从索引3中删除“F”
  • 我试过的 我尝试过使用
    ToObservableChangeSet
    扩展,但它没有适合我的情况的正确行为

    代码
    主题源=新主题();
    IObservable observableOfList=源;
    IOBServableObservableOfchangeSet=source.ToobbservableChangeSet();
    可观变化集
    .Bind(out ReadOnlyObservableCollection boundCollection)
    .Subscribe();
    ((INotifyCollectionChanged)boundCollection).CollectionChanged+=OnCollectionChanged;
    来源.OnNext(新[…]{“A”、“C”、“F”});
    来源.OnNext(新[…]{“A”、“B”、“C”});
    CollectionChanged无效(对象发送方,NotifyCollectionChangedEventArgs e)
    {
    Debug.WriteLine($“[{string.Join(“,”,(IEnumerable)sender)}](操作:{e.Action}位于索引{e.NewStartingIndex})处);
    }
    
    输出 正如我们所见,
    [A,C,F,A,B,C]
    [A,B,C]
    不同


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

    代码
    主题源=新主题();
    IObservable observableOfList=源;
    IObservable observeofchangeset=observechechangeset.Create(列表=>
    {
    返回可观测飞行时间
    .Subscribe(items=>list.EditDiff(items,EqualityComparer.Default));
    });
    可观变化集
    .Bind(out ReadOnlyObservableCollection boundCollection)
    .Subscribe();
    ((INotifyCollectionChanged)boundCollection).CollectionChanged+=OnCollectionChanged;
    来源.OnNext(新[…]{“A”、“C”、“F”});
    来源.OnNext(新[…]{“A”、“B”、“C”});
    CollectionChanged无效(对象发送方,NotifyCollectionChangedEventArgs e)
    {
    Debug.WriteLine($“[{string.Join(“,”,(IEnumerable)sender)}](操作:{e.Action}位于索引{e.NewStartingIndex})处);
    }
    
    输出 正如我们所见,
    [A,C,B]
    [A,B,C]
    不同


    谢谢

    我对DynamicData不太熟悉,但我相信这会带来接近预期的结果:

    使用动态CDATA;
    使用制度;
    使用System.Collections.Generic;
    使用System.Reactive.Linq;
    使用系统、反应、主题;
    名称空间控制台AP3
    {
    班级计划
    {
    静态void Main(字符串[]参数)
    {
    主题来源=新主题();
    var list=observeChangeSet.Create(sourceList=>
    {
    返回源
    .订阅(newItems=>
    {
    编辑(项目=>
    {
    对于(var i=0;i
    不幸的是,我没有弄清楚如何在索引1添加“B”,但当前的输出如下所示:

  • AddRange.3更改
  • 3项更改
    • 替换。当前:B,以前:C
    • 替换。当前:C,以前:F
    • Add.Current:F,Previous:
  • Remove。当前:C,上一个:
  • 编辑:下面给出了您想要的确切输出,但我尚未测试每个输出的性能(对于较大的列表,插入可能会很昂贵):

    使用动态CDATA;
    使用制度;
    使用System.Collections.Generic;
    使用System.Reactive.Linq;
    使用系统、反应、主题;
    名称空间控制台AP3
    {
    班级计划
    {
    静态void Main(字符串[]参数)
    {
    主题来源=新主题();
    var list=observeChangeSet.Create(sourceList=>
    {
    返回源
    .订阅(newItems=>
    {
    编辑(项目=>
    {
    对于(var i=0;i
  • AddRange.3更改
  • Add.当前:B,以前:索引1
  • 删除。当前:C,上一个:索引2

  • 我对DynamicATA不太熟悉,但我相信这会带来接近预期的结果:

    使用动态CDATA;
    使用制度;
    使用System.Collections.Generic;
    使用System.Reactive.Linq;
    使用系统、反应、主题;
    名称空间控制台AP3
    {
    班级计划
    {
    静态void Main(字符串[]参数)
    {
    主题来源=新主题();
    var list=observeChangeSet.Create(sourceList=>
    {
    返回源
    .潜艇
    
    [A, C, F] (operation: Reset at index -1)
    [A, C, F, A] (operation: Add at index 3)
    [A, C, F, A, B] (operation: Add at index 4)
    [A, C, F, A, B, C] (operation: Add at index 5)
    
    [A, C, F] (operation: Reset at index -1)
    [A, C] (operation: Remove at index -1)
    [A, C, B] (operation: Add at index 2)
    
    Subject<IReadOnlyList<string>> source = new Subject<IReadOnlyList<string>>();
    IObservable<IEnumerable<string>> observableOfList = source;
    IObservable<IChangeSet<(string,int),string>> observableOfChangeSet = source.Select(x => 
                {
                    return x.Select((item, index) => (item, index));
                }).ToObservableChangeSet<(string,int),string>(x=>x.Item1);
    
    observableOfChangeSet
        .Sort(SortExpressionComparer<(string, int)>.Ascending(x => x.Item2))
        .OnItemAdded(x=> Debug.WriteLine($"Item {x.Item1} was added to index {x.Item2}"))
        .OnItemRemoved(x => Debug.WriteLine($"Item {x.Item1} was removed from index {x.Item2}"))
        .OnItemUpdated((x,prev) => Debug.WriteLine($"Item {prev.Item1} at index {prev.Item2} was updated to Item {x.Item1} at index {x.Item2}"))
        .Transform(x=>
            {
                Debug.WriteLine($"Item: {x.Item1} at index: {x.Item2}");
               return x.Item1;
            })
        .Bind(out ReadOnlyObservableCollection<string> boundCollection)
        .Subscribe();
    
    source.OnNext(new[] { "A", "C", "F" });
    source.OnNext(new[] { "A", "B", "C" });
    
    Item A was added to index 0
    Item C was added to index 1
    Item F was added to index 2
    Item: A at index: 0
    Item: C at index: 1
    Item: F at index: 2
    Item B was added to index 1
    Item F was removed from index 2
    Item A at index 0 was updated to Item A at index 0
    Item C at index 1 was updated to Item C at index 2
    Item: A at index: 0
    Item: B at index: 1
    Item: C at index: 2