/// <summary> /// Hooks up the element with the given named event. /// </summary> /// <param name="obj"> /// The object to hook the event to. /// </param> /// <param name="eventName"> /// The name of the event to hook up. /// </param> public void HookEvent(object obj, string eventName) { this.eventInfo = GetEventInfo(obj.GetType(), eventName); if (this.eventInfo != null) { this.handler = this.GetEventHandler(this.eventInfo); if (this.eventInfo.AddMethod.ReturnType == typeof(EventRegistrationToken)) { WindowsRuntimeMarshal.AddEventHandler( e => { this.eventRegistrationToken = (EventRegistrationToken)this.eventInfo.AddMethod.Invoke(obj, new object[] { e }); return(this.eventRegistrationToken.Value); }, e => this.eventInfo.RemoveMethod.Invoke(obj, new object[] { e }), this.handler); } else { this.eventInfo.AddMethod.Invoke(obj, new object[] { this.handler }); } } }
/// <summary> /// Adds the specified event handler to the specified element. /// </summary> /// <param name="element">The element to which the specified event handler is added.</param> /// <param name="handler">The event handler to add.</param> /// <param name="handledEventsToo"> /// <c>true</c> to register the handler such that it is invoked even when the /// event is marked handled in its event data; <c>false</c> to register the /// handler with the default condition that it will not be invoked if the event /// is already marked handled. /// </param> protected override void AddEventHandler(FrameworkElement element, Delegate handler, bool handledEventsToo) { if (element == null || handler == null || isEventHandlerAdded) { return; } if (IsRoutedEventHandler(handler)) { element.AddHandler(routedEvent, handler, handledEventsToo); } else if (eventInfo != null) { token = (EventRegistrationToken?)(eventInfo.AddMethod.Invoke(element, new object[] { handler })); } isEventHandlerAdded = true; }
public ContentDialogWrapper(FrameworkElement window) { _window = window; if (_closingEvent == null) { _closingEvent = window.GetType().GetRuntimeEvent("Closing"); } Delegate handler = ServiceProvider .ReflectionManager .TryCreateDelegate(_closingEvent.EventHandlerType, this, OnClosingMethod); if (handler == null) { Tracer.Error("The provider cannot create delegate for event '{0}'", _closingEvent.EventHandlerType); return; } _token = (EventRegistrationToken)_closingEvent.AddMethod.InvokeEx(window, handler); }
/// <summary> /// Unhooks the element with the given named event. /// </summary> /// <param name="obj"> /// The object to unhook the event from. /// </param> public void UnhookEvent(object obj) { if (this.eventInfo != null && this.handler != null) { if (this.eventInfo.AddMethod.ReturnType == typeof(EventRegistrationToken)) { if (this.eventRegistrationToken != null) { this.eventInfo.RemoveMethod.Invoke(obj, new object[] { this.eventRegistrationToken }); this.eventRegistrationToken = null; } } else { this.eventInfo.RemoveMethod.Invoke(obj, new object[] { this.handler }); } this.handler = null; this.eventInfo = null; } }
public ContentDialogWrapper(FrameworkElement window) { _window = window; if (_closingEvent == null) _closingEvent = window.GetType().GetRuntimeEvent("Closing"); Delegate handler = ServiceProvider .ReflectionManager .TryCreateDelegate(_closingEvent.EventHandlerType, this, OnClosingMethod); if (handler == null) { Tracer.Error("The provider cannot create delegate for event '{0}'", _closingEvent.EventHandlerType); return; } _token = (EventRegistrationToken)_closingEvent.AddMethod.InvokeEx(window, handler); }
/// <summary> /// Unhooks the element with the given named event. /// </summary> /// <param name="obj"> /// The object to unhook the event from. /// </param> public void UnhookEvent(object obj) { if (this.eventInfo != null && this.handler != null) { if (this.eventInfo.AddMethod.ReturnType == typeof(EventRegistrationToken)) { if (this.eventRegistrationToken != null) { this.eventInfo.RemoveMethod.Invoke(obj, new object[] { this.eventRegistrationToken }); this.eventRegistrationToken = null; } } else { this.eventInfo.RemoveMethod.Invoke(obj, new object[] { this.handler }); } this.handler = null; this.eventInfo = null; } }
/// <summary> /// Hooks up the element with the given named event. /// </summary> /// <param name="obj"> /// The object to hook the event to. /// </param> /// <param name="eventName"> /// The name of the event to hook up. /// </param> public void HookEvent(object obj, string eventName) { this.eventInfo = GetEventInfo(obj.GetType(), eventName); if (this.eventInfo != null) { this.handler = this.GetEventHandler(this.eventInfo); if (this.eventInfo.AddMethod.ReturnType == typeof(EventRegistrationToken)) { WindowsRuntimeMarshal.AddEventHandler( e => { this.eventRegistrationToken = (EventRegistrationToken)this.eventInfo.AddMethod.Invoke(obj, new object[] { e }); return this.eventRegistrationToken.Value; }, e => this.eventInfo.RemoveMethod.Invoke(obj, new object[] { e }), this.handler); } else { this.eventInfo.AddMethod.Invoke(obj, new object[] { this.handler }); } } }