我正在尝试在RemoveAll上实现AsyncObservableCollection,该类是为在另一个线程上编辑集合而创建的,其结构是这样的:

public class AsyncObservableCollection<T> : ObservableCollection<T>
{
    private readonly SynchronizationContext _synchronizationContext = SynchronizationContext.Current;

    public AsyncObservableCollection()
    {
    }

    public AsyncObservableCollection(IEnumerable<T> list)
        : base(list)
    {
    }

    private void ExecuteOnSyncContext(Action action)
    {
        if (SynchronizationContext.Current == _synchronizationContext)
        {
            action();
        }
        else
        {
            _synchronizationContext.Send(_ => action(), null);
        }
    }

    protected override void InsertItem(int index, T item)
    {
        ExecuteOnSyncContext(() => base.InsertItem(index, item));
    }

    protected override void RemoveItem(int index)
    {
        ExecuteOnSyncContext(() => base.RemoveItem(index));
    }

    protected override void SetItem(int index, T item)
    {
        ExecuteOnSyncContext(() => base.SetItem(index, item));
    }

    protected override void MoveItem(int oldIndex, int newIndex)
    {
        ExecuteOnSyncContext(() => base.MoveItem(oldIndex, newIndex));
    }

    protected override void ClearItems()
    {
        ExecuteOnSyncContext(() => base.ClearItems());
    }

    public void RemoveAll(Predicate<T> predicate)
    {
        CheckReentrancy();

        List<T> itemsToRemove = Items.Where(x => predicate(x)).ToList();
        itemsToRemove.ForEach(item => Items.Remove(item));

        OnPropertyChanged(new PropertyChangedEventArgs("Count"));
        OnPropertyChanged(new PropertyChangedEventArgs("Item[]"));
        OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
    }
}

实际上,如果在主线程上使用集合,则该方法运行良好,但是如果在其他线程上使用,则会得到:



在这条线上:
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));

有什么我可以解决的吗?

最佳答案

我不确定该类(class)的目的以及为什么需要保留当前上下文,但是无论如何,解决问题的方法是

public class ObservableCollectionAsync<T> : ObservableCollection<T>
{
    public ObservableCollectionAsync()
    {
        BindingOperations.EnableCollectionSynchronization(this, _lock);
    }

    public ObservableCollectionAsync(List<T> list) : base(list)
    {
        BindingOperations.EnableCollectionSynchronization(this, _lock);
    }

    public ObservableCollectionAsync(IEnumerable<T> collection) : base(collection)
    {
        BindingOperations.EnableCollectionSynchronization(this, _lock);
    }
    private readonly SynchronizationContext _synchronizationContext = SynchronizationContext.Current;
    private readonly object _lock = new object();
    private void ExecuteOnSyncContext(Action action)
    {
        if (SynchronizationContext.Current == _synchronizationContext)
        {
            action();
        }
        else
        {
            _synchronizationContext.Send(_ => action(), null);
        }
    }

    protected override void ClearItems()
    {
        ExecuteOnSyncContext(() => base.ClearItems());
    }

    protected override void InsertItem(int index, T item)
    {
        ExecuteOnSyncContext(() => base.InsertItem(index, item));
    }

    protected override void MoveItem(int oldIndex, int newIndex)
    {
        ExecuteOnSyncContext(() => base.MoveItem(oldIndex, newIndex));
    }

    protected override void RemoveItem(int index)
    {
        ExecuteOnSyncContext(() => base.RemoveItem(index));
    }

    protected override void SetItem(int index, T item)
    {

        ExecuteOnSyncContext(() => base.SetItem(index, item));


    }

    public void RemoveAll(Func<T,bool> predicate)
    {
        CheckReentrancy();
        foreach (var item in this.Where(predicate).ToArray())
        {
            this.Remove(item);
        }
    }

但是Observable集合本身内的任何其他方法都不会通过“ExecuteOnSyncContext”,因此最好将其公开,并在类外为其他方法调用它。

否则,您将需要使用ICollection接口(interface)构建一个Observable Collection。

关于c# - 在AsyncObservableCollection上实现RemoveAll,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47287326/

10-12 02:57