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.
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!