如何使用 Reactive Extensions 处理派生列表

How to handle derived lists using Reactive Extensions

给定一个具有软删除功能的简单项目

public class Person : ReactiveObject
{
    [Reactive] public string Name { get; set; }
    [Reactive] public bool IsDeleted { get; set; }
}

我需要处理删除的更新并删除 UI 上的项目(同时将它们保留在列表中)。

当我尝试这样做时,已删除的项目没有从派生列表中过滤掉。

public class PeopleViewModel : ReactiveObject
{
    public PeopleViewModel()
    {
        DeleteCommand = ReactiveCommand.Create(DeletePerson);
        this.WhenAnyValue(vm => vm.People)
            .Subscribe(people => people
                .ToObservableChangeSet()
                .Filter(x => !x.IsDeleted)
                .ToCollection()
                .Select(x => new ObservableCollection<Person>(x))
                .ToPropertyEx(this, x => x.VisiblePerson));
    }

    private void DeletePerson(Person person)
    {
        person.IsDeleted = true;
    }

    ICommand DeleteCommand { get; }
    [Reactive] public ObservableCollection<Person> People { get; set;}
    public ObservableCollection<Person> VisiblePeople { [ObservableAsProperty] get;}
}

创建派生列表的正确方法是什么,当列表中的 属性 发生变化时,它会触发 ObservableChangeSet 更新?

------------------------------------------
|  John Smith                   [delete] |
------------------------------------------
|  Jane Doe                     [delete] |
------------------------------------------

编辑

我确实让它与以下内容一起工作,但这感觉真的很脏

this.WhenAnyValue(vm => vm.People)
            .Subscribe(people => people
                .ToObservableChangeSet()
                .AsObservableList()
                .Connect()
                .WhenPropertyChanged(x => x.Deleted)
                .Subscribe(_ => people
                    .ToObservableChangeSet()
                    .Filter(x => !x.Deleted)
                    .ToCollection()
                    .Select(x => x.ToList().AsReadOnly())
                    .DistinctUntilChanged()
                    .ToPropertyEx(this, x => x.VisiblePeople))

所以我对 .Bind(out) 没有以某种方式通知 UI 更改这一事实感到困惑。事实证明,这是完成派生列表所需的一切的正确方法。

ctor()
{
    this.WhenAnyValue(vm => vm.People)
        .Subscribe(people => people
            .ToObservableChangeSet()
            .AutoRefresh(x => x.Deleted)
            .Filter(x => !x.Deleted)
            .Bind(out _visiblePeople)
            .Subscribe());
}

private ReadOnlyObservableCollection<Person> _visiblePeople;
public ReadOnlyObservableCollection<Person> VisiblePeople => _visiblePeople;