BlockReentrancy中的ObservableCollection&LT; T&GT; [英] BlockReentrancy in ObservableCollection<T>
问题描述
可能有人请仁慈地向我解释什么 BlockReentrancy
法的目的是在的ObservableCollection&LT; T&GT;
?
Could someone please be kind enough to explain to me what the purpose of the BlockReentrancy
Method is in the ObservableCollection<T>
?
MSDN 显示以下为例:
//The typical usage is to wrap an OnCollectionChanged call within a using scope, as in the following example:
using (BlockReentrancy())
{
// OnCollectionChanged call
}
不过,这似乎并没有澄清,我的目的是什么。有人在乎解释?
But this doesn't seem to clarify for me what the purpose is. Anyone care to explain?
推荐答案
这是的ObservableCollection
工具 INotifyCollectionChanged
,所以它有一个 CollectionChanged
事件。如果有一个用户这个事件,他们的进一步修改的集合,而集合已经在通知的过程。因为 CollectionChanged
事件跟踪的究竟是什么改变,这种相互作用可能会变得非常混乱。
An ObservableCollection
implements INotifyCollectionChanged
and so it has a CollectionChanged
event. If there is a subscriber to this event, they could further modify the collection while the collection is already in the process of notification. Since the CollectionChanged
event keeps track of exactly what changed, this interaction can get very messy.
其结果是,在的ObservableCollection
允许的话,作为一个特殊的情况下, CollectionChanged
事件的单个订户从它的处理程序修改集合。但它的不允许修改从 CollectionChanged
处理程序集合中的如果有两个或更多的用户到 CollectionChanged
事件。
As a result, the ObservableCollection
allows, as a special case, a single subscriber of the CollectionChanged
event to modify the collection from its handler. But it disallows modifying the collection from the CollectionChanged
handler if there are two or more subscribers to the CollectionChanged
event.
一对方法 BlockReentrancy
和 CheckReentancy
用来实现这个逻辑。该 BlockReentrancy
被用在 OnCollectionChanged
方法的开始和 CheckReentancy
用于修改集合中的所有方法。
The pair of methods BlockReentrancy
and CheckReentancy
are used to implement this logic. The BlockReentrancy
is used at the start of the OnCollectionChanged
method and CheckReentancy
is used in all methods that modify the collection.
这篇关于BlockReentrancy中的ObservableCollection&LT; T&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!