Custom Button Renderer custom events not firing

Hi,
I am trying to add some custom events to button. I need Up, Down, Enter, Exit events.

Here is my code:

`
using System;
using System.Collections.Generic;
using System.Text;
using Xamarin.Forms;

namespace TEST.Controls
{
public class CustomButton : Button
{
#region Signals

    //public event EventHandler Clicked;
    public event EventHandler LongClicked;
    public event EventHandler Down;
    public event EventHandler Up;
    public event EventHandler Exit;
    public event EventHandler Enter;

    //public void SendClicked()
    //{
        //EventHandler eventHandler = this.Clicked;
        //eventHandler?.Invoke((object)this, EventArgs.Empty);

        //Console.WriteLine("Clicked");
    //}
    public void SendLongClicked()
    {
        EventHandler eventHandler = this.LongClicked;
        eventHandler?.Invoke((object)this, EventArgs.Empty);
        Console.WriteLine("LongClicked");
    }
    public void SendDownClicked()
    {
        EventHandler eventHandler = this.Down;
        eventHandler?.Invoke((object)this, EventArgs.Empty);
        Console.WriteLine("Down");
    }
    public void SendUpClicked()
    {
        EventHandler eventHandler = this.Up;
        eventHandler?.Invoke((object)this, EventArgs.Empty);
        Console.WriteLine("Up");
    }
    public void SendExitClicked()
    {
        EventHandler eventHandler = this.Exit;
        eventHandler?.Invoke((object)this, EventArgs.Empty);
        Console.WriteLine("Exit");
    }
    public void SendEnterClicked()
    {
        EventHandler eventHandler = this.Enter;
        eventHandler?.Invoke((object)this, EventArgs.Empty);
        Console.WriteLine("Enter");
    }

    #endregion
}

}
`

and I extend this class in ios like that:

`
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TEST.iOS;
using TEST.Controls;
using TEST.Controls.Renderers;
using Foundation;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;

[assembly: ExportRendererAttribute(typeof(CustomButton), typeof(CustomButtonRenderer))]

namespace TEST.Controls.Renderers
{
public class CustomButtonRenderer : ButtonRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs e)
{
base.OnElementChanged(e);
var view = (CustomButton)Element;
if (view == null) return;

        view.LongClicked += LongClicked;
        view.Clicked += Clicked;
        view.Enter += Enter;
        view.Exit += Exit;
        view.Up += Up;
        view.Down += Down;
    }

    private void Down(object sender, EventArgs e)
    {
        var view = (CustomButton)Element;
        view.SendDownClicked();
    }

    private void Up(object sender, EventArgs e)
    {
        var view = (CustomButton)Element;
        view.SendUpClicked();
    }

    private void Exit(object sender, EventArgs e)
    {
        var view = (CustomButton)Element;
        view.SendExitClicked();
    }

    private void Enter(object sender, EventArgs e)
    {
        var view = (CustomButton)Element;
        view.SendEnterClicked();
    }

    private void Clicked(object sender, EventArgs e)
    {
        System.Diagnostics.Debug.WriteLine("foo");
        //var view = (CustomButton)Element;
        //view.Send();
    }

    private void LongClicked(object sender, EventArgs e)
    {
        var view = (CustomButton)Element;
        view.SendLongClicked();
    }
}

}
`

Not sure what is wrong but only clicked button works. I added breakpoints in the methods Down, Up, Exit, LongClicked and nothing ever happens.
Anyone knows how to fix it?

Thanks in advance.

Best Answer

  • AleksanderDejewskiAleksanderDejewski US ✭✭
    Accepted Answer

    Thanks, this made me think about it. Of course you are right. Added UIButton inside OnElementChanged and added events to it.

Answers

  • JohnHJohnH GBMember ✭✭✭✭✭

    I don't see anything in your renderer that knows about the native interaction? Where do you detect a LongClicked has occurred?

  • AleksanderDejewskiAleksanderDejewski USMember ✭✭
    Accepted Answer

    Thanks, this made me think about it. Of course you are right. Added UIButton inside OnElementChanged and added events to it.

  • JohnHJohnH GBMember ✭✭✭✭✭

    @AleksanderDejewski said:
    Thanks, this made me think about it. Of course you are right. Added UIButton inside OnElementChanged and added events to it.

    Excellent, glad I helped. Gave you a Like too for posting back with the solution you came up with. B)

Sign In or Register to comment.