优化添加/删除/更新集合中新项目的方式

时间:2015-11-06 14:06:13

标签: c# wpf mvvm

我想问我是否仍然可以优化我的代码,因为目前,这就是我写它的方式。

这是使用MVVM,ObservableCollection和C#

的方式

这是伪代码

// ClientCollection is an ObservableCollection<T> object
// ClientLists is a List<T> object that came from the JSON data
//
// check our clients in ClientCollection
// if a client was removed in List<T>, then delete this client
// in our ClientCollection.
ToRemoveLists toremove;
{ 
    for each Clients client in ClientCollection
        var a = from b in ClientLists
                where b.name == client.name
                select b;

        if a.count() == 0 
            toremove.Add a;
}
{ // remove client from ClientCollection
    for each Clients client in toremove
        ClientCollection.Remove client;
}

// now add new clients if there are any
// or update client info if this client exists
{ 
    for each Clients client in ClientLists
        var a = from b in ClientCollection
                where b.name == client.name
                select b;

        if a.count() == 0 
            ClientCollection.Add clients
        else
            // update client info
            // .
            // .
            // .
}

我这样做的原因是因为使用.Clear()看起来不是更新集合的好方法,首先它刷新整个列表,当你有数千个项目时它看起来像闪烁..所以这就是我做的原因。

收了一堆

1 个答案:

答案 0 :(得分:0)

有很多东西可以帮助提高性能,但这是一个很好的起点,适合您的问题。继承自ObservableCollection并提供进行批量更改的方法,但只引发单个事件,从而减少绑定更新:

public class BulkObservableCollection<T> : ObservableCollection<T>
{
    public BulkObservableCollection()
    {
    }

    public BulkObservableCollection(List<T> list) : base(list)
    {
    }

    public BulkObservableCollection(IEnumerable<T> collection) : base(collection)
    {
    }

    /// <summary>
    /// Adds a range if items to the collection, causing a reset event to be fired.
    /// </summary>
    /// <param name="items"></param>
    public void AddRange(IEnumerable<T> items)
    {
        if (items == null) throw new ArgumentNullException("items");

        var intialCount = Items.Count;
        Items.AddRange(items);            
        if (Items.Count != intialCount)
            OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
    }

    /// <summary>
    /// Removes a range of items from the collection, causing a reset event to be fired.
    /// </summary>
    /// <param name="items"></param>
    public void RemoveRange(IEnumerable<T> items)
    {
        if (items == null) throw new ArgumentNullException("items");

        var intialCount = Items.Count;
        foreach (var item in items)            
            Items.Remove(item);
        if (Items.Count != intialCount)
            OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
    }

    public void Reset(IEnumerable<T> newItems)
    {
        if (newItems == null) throw new ArgumentNullException("newItems");

        Items.Clear();
        Items.AddRange(newItems);

        OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
    }
}