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.
Assembly: WindowsBase (in WindowsBase.dll)
You typically use the weak event pattern when the event source has an object lifetime that is independent of the event listeners. Using the central event dispatching capability of a 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 sources for data bindings.
The weak event pattern can also be used for callbacks and regular events.Notes to Inheritors:
Classes that derive from class should do the following:
Provide a static AddListener method. Sources call AddListener to add a listener for the managed weak event. Your implementation calls the ProtectedAddListener method to implement this behavior.
Provide a static RemoveListener method. Sources call RemoveListener to add a listener for the managed event. Your implementation calls the ProtectedRemoveListener method to implement this behavior.
Override the StartListening method to cast the source to the type that owns the event, and connect the handler on the source to the event being managed.
Override the StopListening method to cast the source to the type that owns the event, and disconnect the handler on the source to the event being managed.
Implement the handler, which should call the DeliverEvent event, so that the managed event is forwarded to its weak event pattern listeners.
Provide a CurrentManager property that returns the specific manager type being implemented. The get accessor for CurrentManager should call the GetCurrentManager method to make sure that there is not already an initialized instance. If so, that instance is returned properly typed. If there is no initialized instance, the get accessor should call the SetCurrentManager method to initialize one.