银光ObservableCollection

银光ObservableCollection

本文介绍了银光ObservableCollection:在UI线程上提高CollectionChanged的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在UI线程上引发ObservableCollection的CollectionChanged事件.

I need to raise the CollectionChanged event of an ObservableCollection on the UI thread.

我看到了从包装器类到相关接口的自定义实现的各种方法.

I have seen different approaches ranging from a wrapper class to custom implementation of the relevant interface.

是否有任何简单的方法可以覆盖ObservableCollection上的INotifyCollectionChanged以实现此目的?

Is there any simple way to override INotifyCollectionChanged on an ObservableCollection to accomplish this?

谢谢.

推荐答案

最简单的方法是确保在UI线程上添加/删除集合中的项目.可以使用以下简短函数来完成此操作:

The simplest way to do this is to just ensure that you add/remove items from the collection on the UI thread. This can be done with a short function like this:

private void AddItemsToCollection(List<whatever> newItems)
{
    if (this.Dispatcher.CheckAccess())
    {
        newItems.ForEach(x => myObservableCollection.Add(x));
    }
    else
        this.Dispatcher.BeginInvoke(new Action<List<whatever>>(AddItemsToCollection), newItems);
}

这篇关于银光ObservableCollection:在UI线程上提高CollectionChanged的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 21:21