mardi 9 juillet 2019

Adding an EventHandler to event with reflection in Xamarin.Forms

I am trying to add an EventHandler to an event declared in VisualElement with reflection (A distant parent class in Xamarin.Forms), but it won't be called.

I am assigning it in a constructor of my class AutocompleteController:

public AutocompleteController(InputView inputView)
{
    /* ... */
    var focusChange = typeof(VisualElement).GetEvent("FocusChangeRequested");
    var eventTest = typeof(VisualElement).GetEvent("PropertyChanged");

    focusChange.AddEventHandler(inputView,
        Delegate.CreateDelegate(type: focusChange.EventHandlerType,
        target: typeof(AutocompleteController), method: "FocusChangeRequested"));
    eventTest.AddEventHandler(inputView,
        Delegate.CreateDelegate(type: eventTest.EventHandlerType,
        target: typeof(AutocompleteController), method: "PropertyChanged"));
}

And I have included eventTest just for testing - which work.

Here is the event handlers, where FocusChangeRequested doesn't get called:

public static void FocusChangeRequested(object sender, FocusRequestArgs e)
{
    Console.WriteLine(" Won't fire ");   
}

public static void PropertyChanged(object s, PropertyChangedEventArgs p)
{
    Console.WriteLine("gets called");
}

The event is declared here, like so:

enter image description here

What could be the problem?





Aucun commentaire:

Enregistrer un commentaire