Class WeakEventManager<T>
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.
Inheritance
Namespace: System.Windows
Assembly: Telerik.Windows.Controls.dll
Syntax
public abstract class WeakEventManager<T> : Object where T : class
Type Parameters
T
The type name. |
Constructors
WeakEventManager()
Initializes a new instance of the WeakEventManager class. Creates the base values when it is used as the initializer by the constructor of a derived class.
Declaration
protected WeakEventManager()
Methods
CheckForNull(Object, String)
Helper method that throw ArgumentNullException if given source is null.
Declaration
protected static void CheckForNull(object source, string argumentName)
Parameters
System.Object
source
The element that is check for null. |
System.String
argumentName
The name that should be passed to ArgumentNullException constructor. |
DeliverEvent(Object, EventArgs)
Delivers the event being managed to each listener.
Declaration
protected void DeliverEvent(object sender, EventArgs args)
Parameters
System.Object
sender
The object on which the event is being handled. |
System.EventArgs
args
An EventArgs that contains the event data for the event to deliver. |
Remarks
Call the DeliverEvent method from within the event handlers that are added or removed by the StartListening and StopListening implementations of subclasses. If you call the ProtectedAddListener method in your AddListener implementation of your class, the list of listeners receiving the event is kept in an underlying collection. (AddListener is not part of an interface or class contract. AddListener is the suggested name for the method of your manager class that calls ProtectedAddListener and adds weak event pattern listeners for the event.) ProtectedAddListener adds listeners to a single list. If your manager implementation maintains more than one list of listeners per event, do not use DeliverEvent or ProtectedAddListener. Instead, your implementation should create its own WeakEventManager.ListenerList instances, AddListener should add listeners to the appropriate list, and events should be delivered to the appropriate listener list by calling DeliverEventToList.
DeliverEventToList(Object, EventArgs, WeakEventManager<T>.ListenerList)
Delivers the event being managed to each listener in the provided list.
Declaration
protected void DeliverEventToList(object sender, EventArgs args, WeakEventManager<T>.ListenerList list)
Parameters
System.Object
sender
The object on which the event is being handled. |
System.EventArgs
args
An EventArgs that contains the event data. |
WeakEventManager.ListenerList<>
list
The provided ListenerList. |
Remarks
This method is necessary if your manager implementation maintains separate listeners lists based on information that is captured in the event data. If you use this advanced technique, you must create and maintain the separate lists as part of your manager implementation, and you must provide a way to add listeners to a specific list. Your handler implementation that listens to the raw event must act upon the condition that you use to differentiate the lists, and deliver the event only to the appropriate list or lists.
ProtectedAddListener(T, IWeakEventListener)
Adds the provided listener to the provided source for the event being managed.
Declaration
protected void ProtectedAddListener(T source, IWeakEventListener listener)
Parameters
T
source
The source to attach listeners to. |
IWeakEventListener
listener
The listening class (which must implement IWeakEventListener). |
Remarks
Call this method within your manager class AddListener methods on WeakEventManager implementations. AddListener is the suggested name for the static method you define on your manager class to enable other classes to add a listener for your weak event pattern. AddListener should take two parameters: the source where the listener is attached, and the listener instance. For your AddListener implementation, call the ProtectedAddListener method on the current manager and pass the same two parameters. If the list of listeners was previously empty, ProtectedAddListener calls the StartListening method internally, which will call your specific StartListening override through polymorphism. ProtectedAddListener adds listeners to a single internal WeakEventManager.ListenerList per source. If your manager implementation maintains more than one list of listeners for each event-source combination, do not use ProtectedAddListener. Instead, your implementation should create its own WeakEventManager.ListenerList instances, AddListener should add listeners to the appropriate list, and events should be delivered to the appropriate listener list by calling the DeliverEventToList event instead of the DeliverEvent method.
ProtectedRemoveListener(T, IWeakEventListener)
Removes a previously added listener from the provided source.
Declaration
protected void ProtectedRemoveListener(T source, IWeakEventListener listener)
Parameters
T
source
The source to remove listeners from. |
IWeakEventListener
listener
The listening class (which must implement IWeakEventListener). |
Remarks
Call this method within your manager class RemoveListener methods on WeakEventManager implementations. RemoveListener is the suggested name for the static method you define on your manager class to enable other classes to remove a listener for your weak event pattern. RemoveListener should take two parameters: the source where the listener is removed, and the listener class. For your RemoveListener implementation, call the ProtectedRemoveListener method on the current manager and pass the same two parameters. If a call to ProtectedRemoveListener removes the last listener in the list, ProtectedRemoveListener calls the StopListening method internally, which will call your specific StopListening override through polymorphism. ProtectedRemoveListener removes listeners from a single internal WeakEventManager.ListenerList per source. If your manager implementation maintains more than one list of listeners for each event-source combination, do not use ProtectedRemoveListener. Instead, your implementation should create its own WeakEventManager.ListenerList instances, RemoveListener should remove listeners from the appropriate list, and events should be delivered to the appropriate listener list by calling the DeliverEventToList method instead of the DeliverEvent method.
StartListening(Object)
When overridden in a derived class, starts listening for the event being managed. After the StartListening method is first called, the manager should be in the state of calling DeliverEvent or DeliverEventToList whenever the relevant event from the provided source is handled.
Declaration
protected abstract void StartListening(object source)
Parameters
System.Object
source
The source to begin listening on. |
Remarks
Notes to Inheritors StartListening overrides should add a handler to the provided source. The handler is declared by the manager itself. The class handler should not be public, and it should only be called in response to the event being managed. The class handler should call the DeliverEvent method or the DeliverEventToList method appropriately.
StopListening(Object)
When overridden in a derived class, stops listening on the provided source for the event being managed.
Declaration
protected abstract void StopListening(object source)
Parameters
System.Object
source
The source to stop listening on. |
Remarks
Notes to Inheritors StopListening implementations should remove the class handler as added by the StartListening method. Removing a listener should not clear the listener list. Instead, it should only disconnect the class handler (perhaps temporarily).