| WeakEventHandlerTEventArgsRegisterTSender, TListener Method |
Registers an event handler that works with a weak reference to the target object.
Namespace: DigitalRuneAssembly: DigitalRune (in DigitalRune.dll) Version: 1.20.0.0 (1.20.1.14552)
Syntax public static IDisposable Register<TSender, TListener>(
TSender sender,
TListener listener,
Action<TSender, EventHandler<TEventArgs>> addHandler,
Action<TSender, EventHandler<TEventArgs>> removeHandler,
Action<TListener, Object, TEventArgs> forwardEvent
)
where TSender : class
where TListener : class
Public Shared Function Register(Of TSender As Class, TListener As Class) (
sender As TSender,
listener As TListener,
addHandler As Action(Of TSender, EventHandler(Of TEventArgs)),
removeHandler As Action(Of TSender, EventHandler(Of TEventArgs)),
forwardEvent As Action(Of TListener, Object, TEventArgs)
) As IDisposable
public:
generic<typename TSender, typename TListener>
where TSender : ref class
where TListener : ref class
static IDisposable^ Register(
TSender sender,
TListener listener,
Action<TSender, EventHandler<TEventArgs>^>^ addHandler,
Action<TSender, EventHandler<TEventArgs>^>^ removeHandler,
Action<TListener, Object^, TEventArgs>^ forwardEvent
)
static member Register :
sender : 'TSender *
listener : 'TListener *
addHandler : Action<'TSender, EventHandler<'TEventArgs>> *
removeHandler : Action<'TSender, EventHandler<'TEventArgs>> *
forwardEvent : Action<'TListener, Object, 'TEventArgs> -> IDisposable when 'TSender : not struct when 'TListener : not struct
Parameters
- sender
- Type: TSender
The object that provides the event. - listener
- Type: TListener
The object that listens to the event. - addHandler
- Type: SystemActionTSender, EventHandlerTEventArgs
A callback method that adds an event handler to the event. - removeHandler
- Type: SystemActionTSender, EventHandlerTEventArgs
A callback method that removes an event handler from the event.
- forwardEvent
- Type: SystemActionTListener, Object, TEventArgs
A callback method that forwards the event to the actual event handler.
Type Parameters
- TSender
- The type of the sender.
- TListener
- The type of the listener.
Return Value
Type:
IDisposable
An
IDisposable which can be used to detach the weak event handler from the
event.
Exceptions Remarks
Access to the event and to the real event handler is done through lambda expressions. The
code holds strong references to these expressions, so they must not capture any variables of
the target object (listener)!
Examples WeakEventHandler<DocumentChangeEventArgs>.Register(
textDocument,
this,
(sender, eventHandler) => sender.Changed += eventHandler,
(sender, eventHandler) => sender.Changed -= eventHandler,
(listener, sender, eventArgs) => listener.OnDocumentChanged(sender, eventArgs));
See Also