WeakEventManager Class


為「弱式事件模式」中所使用的事件管理員提供基底類別。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.

Notes to Implementers

如需自訂 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.

(Inherited from 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.

(Inherited from 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.

(Inherited from Object)

傳回用於所提供型別的 WeakEventManager 實作。Returns the WeakEventManager implementation that is used for the provided type.


做為預設雜湊函式。Serves as the default hash function.

(Inherited from Object)

取得目前執行個體的 TypeGets the Type of the current instance.

(Inherited from Object)

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(Inherited from 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.

(Inherited from Object)

請強制執行可以存取這個 DispatcherObject 的呼叫執行緒。Enforces that the calling thread has access to this DispatcherObject.

(Inherited from DispatcherObject)

Applies to

See also