如何判断 ObservableCollection 现在是否在 CollectionChanged 事件中?

How to tell whether ObservableCollection is now in CollectionChanged Event?

在我的应用程序中,我需要检查 ObservableCollection 现在是否在 CollectionChanged 事件中,如果是,我将避免修改集合。

我需要这样做的原因是因为有时我的应用程序会快速连续触发添加或删除事件,甚至在前一个 CollectionChanged 事件完成之前,下一个修改事件已经到来。如果发生这种情况,那么我将获得

Cannot change ObservableCollection during a CollectionChanged event

System: System.InvalidOperationException
Cannot change ObservableCollection during a CollectionChanged event.
   at System.Collections.ObjectModel.ObservableCollection`1.CheckReentrancy()
   at System.Collections.ObjectModel.ObservableCollection`1.InsertItem(Int32 index, T item)
   at System.Collections.ObjectModel.Collection`1.System.Collections.IList.Add(Object value)
   at Syncfusion.Data.CollectionViewAdv.CommitNew()
   at Syncfusion.UI.Xaml.Grid.GridAddNewRowController.CommitAddNew(Boolean changeState)
   at Syncfusion.UI.Xaml.Grid.GridSelectionController.ProcessSourceCollectionChanged(NotifyCollectionChangedEventArgs e, CollectionChangedReason reason)
   at Syncfusion.UI.Xaml.Grid.GridModel.OnSourceCollectionChanged(Object sender, NotifyCollectionChangedEventArgs args)
   at Syncfusion.Data.CollectionViewAdv.RaiseSourceCollectionChangedEvent(NotifyCollectionChangedEventArgs args)
   at Syncfusion.Data.CollectionViewAdv.SourceNotifyCollectionChanged(Object sender, NotifyCollectionChangedEventArgs e)
   at System.Collections.Specialized.NotifyCollectionChangedEventHandler.Invoke(Object sender, NotifyCollectionChangedEventArgs e)
   at System.Collections.ObjectModel.ObservableCollection`1.OnCollectionChanged(NotifyCollectionChangedEventArgs e)
   at System.Collections.ObjectModel.ObservableCollection`1.InsertItem(Int32 index, T item)

在这种情况下,我的应用程序拒绝后续修改事件以避免崩溃是可以的。

如何真正判断前一个 CollectionChanged 事件仍在执行?

我终于设法在一个小样本中重现了这个问题——事实证明这是第三方组件 (Syncfusion) 中的一个错误。

我已联系支持寻求帮助