ReactiveList problems

本文关键字:problems ReactiveList | 更新日期: 2023-09-27 18:06:57

我们对ReactiveUI相对较新,所以这可能解释了为什么我们在获得视图模型工作时遇到一些问题。

在我们的视图模型中,我们有一个类的ReactiveList,该类中有一个'selected'。

在视图模型中,我们想要有一个'AnySelected'属性,这样如果列表中至少有一个项目被标记为selected,那么AnySelected为true。

我们很难得到这项工作。

作为一个只有字符串的小型测试应用程序,我们已经尝试过了,但是没有出现有关发生更改的消息。

public class TestRx : ReactiveObject
{
    private ReactiveList<string> mySelectedItems;
    public ReactiveList<string> MySelectedItems
    {
        get { return mySelectedItems; }
        set { this.RaiseAndSetIfChanged(ref mySelectedItems, value); }
    }
    public TestRx()
    {
        var firstList = new ReactiveList<string>();
        var t = this.WhenAnyValue(x => x.MySelectedItems);
        var t1 = t.Select(x => x ?? new ReactiveList<string>());
        var changed = t1.Select(x => x.Changed.Select(_ => Unit.Default));
        var itemChanged = t1.Select(x => x.ItemChanged.Select(_ => Unit.Default));
        var countChanged = t1.Select(x => x.CountChanged.Select(_ => Unit.Default));
        t.Subscribe(x => Debug.WriteLine("T HAS CHANGED {0}", x == firstList));
        t1.Subscribe(z => Debug.WriteLine("T1 Changed {0}", z == firstList));
        changed.Subscribe(x => Debug.WriteLine("Changed :"));
        itemChanged.Subscribe(x => Debug.WriteLine("Item Changed :"));
        var replacementList = new ReactiveList<SelItem>(new[] {
                new SelItem() { Selected = false } 
        });
        Debug.WriteLine("***********************Assign 1st list");
        MySelectedItems = firstList;
        Thread.Sleep(100);
        Debug.WriteLine("***********************Adding item 2 list");
        MySelectedItems.Add("a new string");
        // we don't get any debug messages  as a result of the above
        Thread.Sleep(100);
        Debug.WriteLine("***********************Assign null");
        MySelectedItems = null;
        Thread.Sleep(100);
    }
}

我们做错了什么?

ReactiveList problems

这是一种常见的模式,但是实现起来有点棘手,因为您必须处理以下所有场景:

  1. 列表设置
  2. 列表项更改
  3. 任何项目的"Selected"属性发生变化。记住,你想看的项目会因为第一条或第二条而改变。

我该怎么做?

这里有一种方法。它很复杂,这暗示了未来版本的RxUI可以做得更好,但这里是你现在可以做的。

IObservable<bool> WhenAnyAreTrue(IEnumerable<ViewModel> currentElements)
{
    // NB: 'Unit' here means, we don't care about the actual value, just
    // that something changed
    var notifyWhenAnySelectedItemChanges = currentElements
        .Select(x => x.WhenAny(y => y.Selected, _ => Unit.Default).Skip(1))
        .Merge();
    return notifyWhenAnySelectedItemChanges
        .StartWith(Unit.Default)
        .Select(_ => currentElements.Any(x => x.Selected));
}
// Any time MySelectedItems change or when the items in it change,
// create a new WhenAnyAreTrue and switch to it
this.WhenAnyObservable(x => x.MySelectedItems.ItemsChanged)
    .Select(_ => WhenAnyAreTrue(MySelectedItems))
    .Switch()
    .ToProperty(this, x => x.AnySelected, out anySelected);