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
继承
WeakEventManager
派生

注解

当事件源具有独立于事件侦听器的对象生存期时,通常使用弱事件模式。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.

构造函数

WeakEventManager()

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

属性

Dispatcher

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

(继承自 DispatcherObject)
Item[Object]

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

ReadLock

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

WriteLock

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

方法

CheckAccess()

确定调用线程是否可以访问此 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.

Equals(Object)

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

(继承自 Object)
GetCurrentManager(Type)

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

GetHashCode()

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

(继承自 Object)
GetType()

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

(继承自 Object)
MemberwiseClone()

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

(继承自 Object)
NewListenerList()

返回包含事件侦听器的新对象。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.

Remove(Object)

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

ScheduleCleanup()

要求在优先级较低的线程上清除基础侦听器列表中未使用的项。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.

StartListening(Object)

在派生类中重写时,开始侦听被托管的事件。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.

StopListening(Object)

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

ToString()

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

(继承自 Object)
VerifyAccess()

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

(继承自 DispatcherObject)

适用于

另请参阅