Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

ObservableCollection.AddRange()

$
0
0

If your Models expose an ObservableCollection, and you are adding lots of elements, you don't want this adding to fire a UI event every time.

You can use this ObservableCollection which only notifies the UI to update after all the elements are updated when AddRange is used:

public class ObservableCollectionFast<T> : ObservableCollection<T>
{
    public ObservableCollectionFast()  : base()   {   }

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

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

    public void AddRange(IEnumerable<T> range)
    {
        foreach (var item in range)
        {
            Items.Add(item);
        }

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

    public void Reset(IEnumerable<T> range)
    {
        this.Items.Clear();

        AddRange(range);
    }
}

Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>