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


(發送器) 會藉由在執行此介面的ReceiveWeakEvent類別上呼叫方法來轉送事件, 並事先呼叫WeakEventManager方法將其新增為接聽程式。 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.