Xamarin 自定义渲染器事件未从 TabbedPage 触发

Xamarin Custom Renderer Event not Firing from TabbedPage

我已经为一个问题苦苦挣扎了很长一段时间,我对 xamarin.我有一个包含多个自定义渲染器的脚本。我的第一个渲染器点击事件完美运行,但所有其他点击事件都没有,它们甚至没有被调用。下面是我的渲染器脚本代码

[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);
    }
}`

在我的 TabbedPage 代码隐藏中,我只是正常调用点击事件,它适用于我的第一个按钮,但不适用于其他任何按钮。

发现,在回答时,xamarin 表单不喜欢将控件嵌套得太深(即嵌套嵌套嵌套嵌套等),因此事件没有触发。我希望这对以后的人有所帮助!