IWeakEventListener 接口

定义

为希望通过 WeakEvent 模式和 WeakEventManager 接收事件的类提供事件侦听支持。Provides event listening support for classes that expect to receive events through the WeakEvent pattern and a WeakEventManager.

public interface class IWeakEventListener
public interface IWeakEventListener
type IWeakEventListener = interface
Public Interface IWeakEventListener
派生

注解

WeakEventManager调度程序) 通过 ReceiveWeakEvent 在实现此接口的类上调用方法,并事先调用方法已将添加为侦听器,来转发事件 (WeakEventManagerThe WeakEventManager (a dispatcher) forwards an event by calling the ReceiveWeakEvent method on classes that implement this interface, and that have been added as listeners by calling WeakEventManager methods beforehand.

遵循 WeakEvent 模式的主要原因是事件源具有可能独立于事件侦听器的对象生存期。The principal reason for following the WeakEvent pattern is when the event source has an object lifetime that is potentially independent of the event listeners. 通过使用的中心事件调度,可对 WeakEventManager 侦听器的处理程序进行垃圾回收 (或手动清除) 即使源对象生存期超出侦听器。Using the central event dispatching of a WeakEventManager enables the handlers for listeners to be garbage collected (or manually purged) even if the source object lifetime extends beyond the listeners. 与此相反,使用 += 或等效特定于语言的事件语法的正常事件挂接会导致可能仍在连接的源持有对处理程序的强引用。By contrast, a normal event hookup using += or equivalent language-specific event syntax causes the potentially still-connected source to hold a strong reference to the handlers. 这会使侦听器引用及时地被垃圾回收。This keeps the listener references from being garbage collected in a timely fashion.

源和侦听器之间的关系建议使用此模式的一种常见情况是,处理来自数据绑定的源的更新事件。One common situation where the relationships between sources and listeners suggests the use of this pattern is the handling of update events coming from sources for data bindings.

可以使用 WeakEvent 模式来侦听应用程序外部的事件。You can use the WeakEvent pattern to listen for events that are external to your application. 只需为 WeakEventManager 其定义,在中提供侦听行为 ReceiveWeakEvent ,并使用 WeakEventManager 附加处理程序引用而不是 +=You only need to define a WeakEventManager for it, provide listening behavior in ReceiveWeakEvent, and use the WeakEventManager to attach the handler references rather than +=.

方法

ReceiveWeakEvent(Type, Object, EventArgs)

接收集中事件管理器中的事件。Receives events from the centralized event manager.

适用于

另请参阅