Tag: addrange

在ObservableCollection上实现AddRange,并对DataBinding提供适当的支持

我想我自己的ObservableCollection后代支持AddRange方法。 这是我目前拥有的: public class ObservableCollectionPlus : ObservableCollection { public void InsertRange(IEnumerable items) { this.CheckReentrancy(); foreach (var item in items) Items.Add(item); var type = NotifyCollectionChangedAction.Reset; var colChanged = new NotifyCollectionChangedEventArgs(type); var countChanged = new PropertyChangedEventArgs(“Count”); OnPropertyChanged(countChanged); OnCollectionChanged(colChanged); } } 我不太了解这里到底发生了什么,为什么会引发这些事件。 这是我在对google和stackoverflow进行一些研究后组装的一个解决方案。 现在,如果我将我的类的实例绑定到LongListSelector然后,在通过InsertRange动态地将项添加到ObservableCollectionPlus ,绑定的LongListSelector的滚动位置将被发送到它的顶部。 如果我以这种标准方式添加项目: foreach (var item in items) collection.Add(item); 那么LongListSelector的位置不会被移位。 但是当然这样我得到的DataBinding通知开销是不受欢迎的。 显然,在我目前的解决方案中出现了问题。 如何实现与foreach (var item […]

List .AddRange实现次优

分析我的C#应用​​程序表明在List.AddRange花费了大量时间。 使用Reflector查看此方法中的代码表明它调用了List.InsertRange ,它实现如下: public void InsertRange(int index, IEnumerable collection) { if (collection == null) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.collection); } if (index > this._size) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_Index); } ICollection is2 = collection as ICollection; if (is2 != null) { int count = is2.Count; if (count > 0) { this.EnsureCapacity(this._size + count); if (index < this._size) { Array.Copy(this._items, index, […]