| WeakEventHandlerTEventHandler, TEventArgsRegisterTSender, 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,
Func<EventHandler<TEventArgs>, TEventHandler> conversion,
Action<TSender, TEventHandler> addHandler,
Action<TSender, TEventHandler> 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,
conversion As Func(Of EventHandler(Of TEventArgs), TEventHandler),
addHandler As Action(Of TSender, TEventHandler),
removeHandler As Action(Of TSender, TEventHandler),
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,
Func<EventHandler<TEventArgs>^, TEventHandler>^ conversion,
Action<TSender, TEventHandler>^ addHandler,
Action<TSender, TEventHandler>^ removeHandler,
Action<TListener, Object^, TEventArgs>^ forwardEvent
)
static member Register :
sender : 'TSender *
listener : 'TListener *
conversion : Func<EventHandler<'TEventArgs>, 'TEventHandler> *
addHandler : Action<'TSender, 'TEventHandler> *
removeHandler : Action<'TSender, 'TEventHandler> *
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. - conversion
- Type: SystemFuncEventHandlerTEventArgs, TEventHandler
A function used to convert the given event handler to a delegate compatible with the
underlying .NET event.
- addHandler
- Type: SystemActionTSender, TEventHandler
A callback method that adds an event handler to the event. - removeHandler
- Type: SystemActionTSender, TEventHandler
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<PropertyChangedEventHandler, PropertyChangedEventArgs>.Register(
textDocument,
this,
handler => new PropertyChangedEventHandler(handler),
(sender, pcHandler) => sender.PropertyChanged += pcHandler,
(sender, pcHandler) => sender.PropertyChanged -= pcHandler,
(listener, sender, eventArgs) => listener.OnDocumentChanged(sender, eventArgs));
See Also