我已经为问题苦苦挣扎了一段时间了,我对xamarin还是很陌生。我有一个包含多个自定义渲染器的脚本。我的第一个Renderers click事件运行得很好,但是其他所有click事件都不能,它们甚至都没有被调用。下面是我的Renderer脚本代码
[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代码中,我只是正常调用click事件,该事件适用于我的第一个按钮,但不适用于其他任何按钮。
答案 0 :(得分:0)
指出,在回答时,xamarin表单不喜欢将控件嵌套得太深(例如,嵌套在嵌套中的嵌套等),因此事件不会触发。我希望这对以后的人有所帮助!