[英]Bindable collection which inserts at top and removes from bottom
I am using an ObservableCollection
to which 25 items will be added per second in a background thread. 我正在使用
ObservableCollection
,它将在后台线程中每秒添加25个项目。 The items are added using Insert
to 0th position. 使用
Insert
到第0个位置添加项目。 After a time period, old items are being removed in a background thread. 一段时间后,旧项目将在后台线程中被删除。 The items at the end are being removed using
RemoveAt
. 最后的项目将使用
RemoveAt
。 This collection is binded to a datagrid. 该集合绑定到一个数据网格。 CPU usage is increasing and the UI is not responsive since these operations are being done.
由于这些操作已完成,因此CPU使用率正在增加,并且UI没有响应。
I have tried creating a custom stack and it won't satisfy the complete requirement as the items cannot be removed from bottom in an efficient way. 我曾尝试创建自定义堆栈,但由于无法高效地从底部将其移除,因此无法满足完整要求。
I need to create a bindable custom collection which inserts item to the top and removes from bottom in a very efficient manner. 我需要创建一个可绑定的自定义集合,该集合以非常有效的方式将项目插入顶部并从底部移除。
Looking forward to any kind of input/suggestions. 期待任何形式的意见/建议。 Thanks.
谢谢。
How about deriving your custom collection from ObservableCollection
and just adding the bare minimum of needed extra functionality? 从
ObservableCollection
派生自定义集合并仅添加最少的所需额外功能怎么样? Something along the lines of 遵循以下原则
class ExtendedObservableCollection<T> : ObservableCollection<T>
{
public void AddRange(int startingIndex, IEnumerable<T> items)
{
var notifier = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, items.ToList(), startingIndex);
foreach (var item in items)
{
// insert to the underlying collection to avoid change events
Items.Insert(startingIndex++, item);
}
OnCollectionChanged(notifier);
}
public void RemoveRange(int startingIndex, int count)
{
// Do it yourself
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.