WeakEventManager 类


为在“弱事件模式”中使用的事件管理器提供基类**。Provides a base class for the event manager that is used in the weak event pattern. 管理器为也使用该模式的事件(或回调)添加和移除侦听器。The manager adds and removes listeners for events (or callbacks) that also use the pattern.

public ref class WeakEventManager abstract : System::Windows::Threading::DispatcherObject
public abstract class WeakEventManager : System.Windows.Threading.DispatcherObject
type WeakEventManager = class
    inherit DispatcherObject
Public MustInherit Class WeakEventManager
Inherits DispatcherObject


当事件源具有独立于事件侦听器的对象生存期时,通常使用弱事件模式。You typically use the weak event pattern when the event source has an object lifetime that is independent of the event listeners. 使用的中心事件调度功能 WeakEventManager 允许对侦听器的处理程序进行垃圾回收,即使源对象仍然存在也是如此。Using the central event dispatching capability of a WeakEventManager allows the listener's handlers to be garbage collected even if the source object persists. 相比之下,使用运算符的定期事件挂接 += 会导致可能断开连接的源持有对侦听器的引用。By contrast, a regular event hookup using the += operator causes the potentially disconnected source to hold a reference to the listeners. 这会阻止接收方及时进行垃圾回收。This prevents the receiver from being garbage collected in a timely fashion.

源和侦听器之间的生存期关系应使用弱事件模式的一种常见情况是处理来自数据绑定的更新事件。One common situation where the lifetime relationships between sources and listeners should use of the weak event pattern is the handling of update events coming from data bindings.

弱事件模式也可用于回调和常规事件。The weak event pattern can also be used for callbacks and regular events.


有关自定义的示例 WeakEventManager ,请参阅 弱事件模式For an example of a custom WeakEventManager, see Weak Event Patterns.



在基类值由派生类的构造函数用作初始值设定项时初始化基类值。Initializes base class values when it is used as the initializer by the constructor of a derived class.



获取与此 Dispatcher 关联的 DispatcherObjectGets the Dispatcher this DispatcherObject is associated with.

(继承自 DispatcherObject)

获取或设置为指定的源存储的数据。Gets or sets the data being stored for the specified source.


在基础数据表上建立读取锁定,并返回 IDisposableEstablishes a read-lock on the underlying data table, and returns an IDisposable.


在基础数据表上建立写入锁定,并返回 IDisposableEstablishes a write-lock on the underlying data table, and returns an IDisposable.



确定调用线程是否可以访问此 DispatcherObjectDetermines whether the calling thread has access to this DispatcherObject.

(继承自 DispatcherObject)
DeliverEvent(Object, EventArgs)

将正在托管的事件传送到每个侦听器。Delivers the event being managed to each listener.

DeliverEventToList(Object, EventArgs, WeakEventManager+ListenerList)

将正在托管的事件传送到所提供列表中的每个侦听器。Delivers the event being managed to each listener in the provided list.


确定指定对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)

返回用于所提供类型的 WeakEventManager 实现。Returns the WeakEventManager implementation that is used for the provided type.


作为默认哈希函数。Serves as the default hash function.

(继承自 Object)

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)

返回包含事件侦听器的新对象。Returns a new object to contain listeners to an event.

ProtectedAddHandler(Object, Delegate)

添加指定的委托为指定源的事件处理程序。Adds the specified delegate as an event handler of the specified source.

ProtectedAddListener(Object, IWeakEventListener)

将提供的侦听器添加到为托管事件所提供的源中。Adds the provided listener to the provided source for the event being managed.

ProtectedRemoveHandler(Object, Delegate)

从指定的源中移除以前添加的处理程序。Removes the previously added handler from the specified source.

ProtectedRemoveListener(Object, IWeakEventListener)

从提供的源中移除以前添加的侦听器。Removes a previously added listener from the provided source.

Purge(Object, Object, Boolean)

从所提供源的数据列表中移除不活动的侦听器项。Removes inactive listener entries from the data list for the provided source. 如果某些项实际上已经从列表中移除,则返回 trueReturns true if some entries were actually removed from the list.


移除指定源的所有侦听器。Removes all listeners for the specified source.


要求在优先级较低的线程上清除基础侦听器列表中未使用的项。Requests that a purge of unused entries in the underlying listener list be performed on a lower priority thread.

SetCurrentManager(Type, WeakEventManager)

为指定的管理器类型设置当前管理器。Sets the current manager for the specified manager type.


在派生类中重写时,开始侦听被托管的事件。When overridden in a derived class, starts listening for the event being managed. 第一次调用 StartListening(Object) 方法后,只要处理了所提供源中的相关事件,管理器就应处于调用 DeliverEvent(Object, EventArgs)DeliverEventToList(Object, EventArgs, WeakEventManager+ListenerList) 的状态。After the StartListening(Object) method is first called, the manager should be in the state of calling DeliverEvent(Object, EventArgs) or DeliverEventToList(Object, EventArgs, WeakEventManager+ListenerList) whenever the relevant event from the provided source is handled.


在派生类中重写时,停止为被托管的事件侦听所提供的源。When overridden in a derived class, stops listening on the provided source for the event being managed.


返回表示当前对象的字符串。Returns a string that represents the current object.

(继承自 Object)

强制调用线程具有此 DispatcherObject 的访问权限。Enforces that the calling thread has access to this DispatcherObject.

(继承自 DispatcherObject)