0

I have been struggling with a problem for quite some time now, I'm fairly new to xamarin. I have a script containing multiple custom renderers. My first Renderers click event works perfectly, but every other click event does not, they don't even get called. Below is my Renderer scripts code

[assembly: ExportRenderer(typeof(CustomLogoutButton),typeof(CustomLogoutButtonRenderer))]
[assembly: ExportRenderer(typeof(CustomRoundButton), typeof(CustomRoundButtonRenderer))]
[assembly: ExportRenderer(typeof(CustomScanButton), typeof(CustomScanButtonRenderer))]
[assembly: ExportRenderer(typeof(CustomTransactionButton), typeof(CustomTransactionButtonRenderer))]
namespace VoucherSystemApp.Droid.CustomRenderClasses
{
public class CustomRoundButtonRenderer : ButtonRenderer
{
    public CustomRoundButtonRenderer(Context context)
        : base(context) { }


    protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Button> e)
    {
        base.OnElementChanged(e);

        var theButton = Control;

        if(theButton != null)
        {
            theButton.Background = Android.App.Application.Context.GetDrawable(Resource.Drawable.RoundCornerButton);

            theButton.Click += TheButton_Click;
        }
    }

    private void TheButton_Click(object sender, EventArgs e)
    {
        ((IButtonController)Element)?.SendClicked();
    }

    protected override void Dispose(bool disposing)
    {

        if(Control != null)
        {
            Control.Click -= TheButton_Click;
        }
        base.Dispose(disposing);
    }
}

public class CustomScanButtonRenderer : ButtonRenderer
{
    public CustomScanButtonRenderer(Context context)
        : base(context) { }

    protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Button> e)
    {
        base.OnElementChanged(e);

        var theButton = Control;

        if(theButton != null)
        {

            theButton.Background = Android.App.Application.Context.GetDrawable(Resource.Drawable.SemiRoundCornerButton);

            theButton.Click += TheButton_Click;
        }
    }

    private void TheButton_Click(object sender, EventArgs e)
    {
        ((IButtonController)Element)?.SendClicked();
    }

    protected override void Dispose(bool disposing)
    {

        if (Control != null)
        {
            Control.Click -= TheButton_Click;
        }
        base.Dispose(disposing);
    }
}

public class CustomTransactionButtonRenderer : ButtonRenderer
{
    public CustomTransactionButtonRenderer(Context context)
        : base(context) { }

    protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Button> e)
    {
        base.OnElementChanged(e);

        var theButton = Control;

        if (theButton != null)
        {
            theButton.Background = Android.App.Application.Context.GetDrawable(Resource.Drawable.TransactionsButton);

            theButton.Click += TheButton_Click;
        }
    }

    private void TheButton_Click(object sender, EventArgs e)
    {
        ((IButtonController)Element)?.SendClicked();
    }

    protected override void Dispose(bool disposing)
    {

        if (Control != null)
        {
            Control.Click -= TheButton_Click;
        }
        base.Dispose(disposing);
    }
}

public class CustomLogoutButtonRenderer : ButtonRenderer
{
    public CustomLogoutButtonRenderer(Context context)
        : base(context) { }

    protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Button> e)
    {
        base.OnElementChanged(e);

        var theButton = Control;

        if (theButton != null)
        {

            theButton.Background = Android.App.Application.Context.GetDrawable(Resource.Drawable.ProfileButton);


            theButton.Click += TheButton_Click;
        }


    }

    private void TheButton_Click(object sender, EventArgs e)
    {
        ((IButtonController)Element)?.SendClicked();
    }

    protected override void Dispose(bool disposing)
    {

        if (Control != null)
        {
            Control.Click -= TheButton_Click;
        }
        base.Dispose(disposing);
    }
}`

and in my TabbedPage Code Behind, i just call the click event as normal, which worked for my first button but not for any of the others.

Blu
  • 821
  • 4
  • 17
Millar
  • 1
  • 5
  • Edit: I just moved the custom renderer and custom class to separate scripts, and still the problem persists. Also, the custom button is on a navigation page(tabbed page) – Millar Feb 13 '20 at 12:57
  • why dont you try making seperate class for each renderer, maybe that would be a work around. – Blu Feb 13 '20 at 14:01
  • I Figured it out, kinda silly, but my custom renderer was nested in about 3 different other controls (relativelayout,stacklayout,absolutelayout). customrendered events wont fire when theres that much nesting xD – Millar Feb 13 '20 at 14:57

1 Answers1

0

Figured out that, at the time of answering, xamarin forms does not like when you nest a control too deeply (i.e. a nest within a nest within a nest etc) and therefore the events weren't firing. I hope this helps someone in the future!

Millar
  • 1
  • 5
  • glad to hear that you have solved your issue by yourself, please remember to mark your reply a answer, it is beneficial to other community members who face the same issue, thanks – Cherry Bu - MSFT Feb 14 '20 at 01:39