Jayson Ragasa
Jayson Ragasa

Reputation: 53

Xamarin.Forms Handling Custom Event in Custom Renderer

I have a basic implementation of a custom render I will be using for handling Long Press.. It's all really based from this code http://arteksoftware.com/gesture-recognizers-with-xamarin-forms/

In my "GestureContainerView" I have an event that I called "OnHeldDown", enter image description here

How do I raise this "OnHeldDown" event if (in my Android) detected a "LongPress" ?

I tried looking up in google but couldn't find an example.

----------- UPDATE ------- (found a solution)

in PCL
enter image description here

in Android
enter image description here

Upvotes: 0

Views: 2560

Answers (1)

Gerald Versluis
Gerald Versluis

Reputation: 33993

Just create a method which checks if anyone is subscribes to the event handler and invoke it, if anyone is.

For example, create a methode like this:

private void RaiseOnHeldDown()
{
    if (OnHeldDown != null)
        OnHeldDown(this, EventArgs.Empty);

    // Or even do the null propagation way
    // OnHeldDown?.Invoke(this, EventArgs.Empty);
}

Of course if you'd like you can supply EventArgs.

Now in the event where you detect the LongPress you just call this method.

Upvotes: 1

Related Questions