WeakEventListener<TInstance, TEventArgs> Class
Provides a listener that allows for "weak" listening to events.
public class WeakEventListener<TInstance, TEventArgs> where TInstance : class where TEventArgs : EventArgs
- Type Parameters:
-
TInstance
-The instance type that owns the listener.
TEventArgs
-The event arguments type.
- Inheritance:
- object object
Remarks
Make sure that only variables (i.e. not fields) for the event source are used when adding/removing event ties;
otherwise this will not result in a "weak" listener.
The instance type should also be disposable and should detach the event listener in the finalizer or a memory leak
of WeakEventListener
objects may occur.
Constructors
WeakEventListener(TInstance)
Initializes a new instance of the WeakEventListener
class.
public WeakEventListener(TInstance instance)
Parameter | Type | Description |
---|---|---|
instance | TInstance | The instance that owns the listener. |
WeakEventListener(TInstance, EventAction<TInstance, object, TEventArgs>, Action<WeakEventListener<TInstance, TEventArgs>>)
Initializes a new instance of the WeakEventListener
class and auto-ties to a related source event.
public WeakEventListener(TInstance instance, WeakEventListener<TInstance, TEventArgs>.EventAction<TInstance, object, TEventArgs> onEventAction, Action<WeakEventListener<TInstance, TEventArgs>> onDetachAction)
Parameter | Type | Description |
---|---|---|
instance | TInstance | The instance that owns the listener. |
onEventAction | WeakEventListener<TInstance, TEventArgs>.EventAction<TInstance, object, TEventArgs> | The delegate to execute when the event is raised. |
onDetachAction | Action<WeakEventListener<TInstance, TEventArgs>> | The delegate to execute when the event is detached. |
Properties
OnDetachAction
Gets or sets the delegate to execute when the event is detached.
public Action<WeakEventListener<TInstance, TEventArgs>> OnDetachAction { get; set; }
Property Value
- Action<WeakEventListener<TInstance, TEventArgs>>:
The delegate to execute when the event is detached.
OnEventAction
Gets or sets the delegate to execute when the event is raised.
public WeakEventListener<TInstance, TEventArgs>.EventAction<TInstance, object, TEventArgs> OnEventAction { get; set; }
Property Value
- WeakEventListener<TInstance, TEventArgs>.EventAction<TInstance, object, TEventArgs>:
The delegate to execute when the event is raised.
Methods
Detach()
Detaches the event listener from the event.
public void Detach()
OnEvent(object, TEventArgs)
The method that handles the event.
public void OnEvent(object source, TEventArgs eventArgs)
Parameter | Type | Description |
---|---|---|
source | object | The source of the event. |
eventArgs | TEventArgs | The event arguments. |