在AsyncObservableCollection中集成Remove <t>?

时间:2017-10-16 10:32:49

标签: c#

我有一个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());
    }
}

我正在尝试添加this metod:

 public static int Remove<T>(
    this ObservableCollection<T> coll, Func<T, bool> condition)
{
    var itemsToRemove = coll.Where(condition).ToList();

    foreach (var itemToRemove in itemsToRemove)
    {
        coll.Remove(itemToRemove);
    }

    return itemsToRemove.Count;
}

但是我收到了这个错误:

  

扩展方法必须在非泛型静态类中定义

我搜索了这个错误,解决方案是将类更改为静态,但我已经尝试过但没有工作。 任何想法?

0 个答案:

没有答案