私はこれに答えざるを得ませんでした。この回答はもう必要ないと思いますが、他の誰かが使用できるかもしれません。
あまり難しく考えないでください (このマルチスレッドにアプローチしないでください (これによりエラーが発生しやすくなり、不必要に複雑になります。難しい計算/IO にのみスレッドを使用してください)、これらすべての異なるアクションタイプにより、バッファリングが非常に困難になります。最も厄介な部分つまり、10000 個の項目を削除または追加すると、アプリケーション (リストボックス) は、ObservableCollection によって発生したイベントの処理で非常に忙しくなります. イベントは既に複数の項目をサポートしています. そう.....
アクションが変更されるまで、アイテムをバッファできます。そのため、「ユーザー」がアクションを変更またはフラッシュすると、追加アクションはバッファリングされ、バッチとして発生します。テストしていませんが、次のようなことができます。
// Written by JvanLangen
public class BufferedObservableCollection<T> : ObservableCollection<T>
{
// the last action used
public NotifyCollectionChangedAction? _lastAction = null;
// the items to be buffered
public List<T> _itemBuffer = new List<T>();
// constructor registeres on the CollectionChanged
public BufferedObservableCollection()
{
base.CollectionChanged += new NotifyCollectionChangedEventHandler(ObservableCollectionUpdate_CollectionChanged);
}
// When the collection changes, buffer the actions until the 'user' changes action or flushes it.
// This will batch add and remove actions.
private void ObservableCollectionUpdate_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
// if we have a lastaction, check if it is changed and should be flush else only change the lastaction
if (_lastAction.HasValue)
{
if (_lastAction != e.Action)
{
Flush();
_lastAction = e.Action;
}
}
else
_lastAction = e.Action;
_itemBuffer.AddRange(e.NewItems.Cast<T>());
}
// Raise the new event.
protected void RaiseCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (this.CollectionChanged != null)
CollectionChanged(sender, e);
}
// Don't forget to flush the list when your ready with your action or else the last actions will not be 'raised'
public void Flush()
{
if (_lastAction.HasValue && (_itemBuffer.Count > 0))
{
RaiseCollectionChanged(this, new NotifyCollectionChangedEventArgs(_lastAction.Value, _itemBuffer));
_itemBuffer.Clear();
_lastAction = null;
}
}
// new event
public override event NotifyCollectionChangedEventHandler CollectionChanged;
}
楽しんでね、J3R03N