如何从Xamarin UWP的EventHandler中删除委托?



我正试图从eventandlerinXamarin中删除Delegate。UWP. Specifically I am trying to implement aCustomRendererfor the Xamarin.FormsEntry, and I would like to remove an event handler delegate that has been assigned to theKeyUpeventandlerforFormsTextBox ' in Xamarin

我已经尝试使用这里找到的信息,以及这里,但似乎在Xamarin不起作用。关于如何实现这一点,有什么想法吗?

我在OnElementChanged覆盖我的CustomRenderer中有这样的东西。

FieldInfo f1 = typeof(Control).GetField("KeyUp",
BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic);

object obj = f1.GetValue(this.Control);

PropertyInfo pi = this.Control.GetType().GetProperty("Events",  
BindingFlags.NonPublic | BindingFlags.Instance);
EventHandlerList list = (EventHandlerList)pi.GetValue(this.Control, null);
list.RemoveHandler(obj, list[obj]);

f1的值总是返回null不确定我做错了什么。

我也尝试使用信息从这里找到正确的事件名称(我确定为KeyUp),但它仍然不起作用。

让它像这样工作

var keyUpRuntimeEvent = this.Control.GetType().GetRuntimeEvent("KeyUp");
Action<EventRegistrationToken> removeEventHandlerAction =
(Action<EventRegistrationToken>)Delegate.CreateDelegate(typeof(Action<EventRegistrationToken>),
this.Control,
keyUpRuntimeEvent.RemoveMethod);
WindowsRuntimeMarshal.RemoveAllEventHandlers(removeEventHandlerAction);
this.Control.KeyUp += TextBoxOnKeyUp;

最新更新