Xamarin表单自定义控件命令未被激发

Xamarin表单自定义控件命令未被激发,xamarin,xamarin.forms,xamarin.android,custom-controls,Xamarin,Xamarin.forms,Xamarin.android,Custom Controls,我正在创建一个特定于平台的自定义按钮,其中的文本和图像在Android和iOS中具有自定义渲染器。我的问题是,我的按钮上的“单击命令”不会被触发 我创建了自定义android渲染器,强制使用自定义android布局 Xamarin.Form按钮类: public static readonly BindableProperty CommandProperty = BindableProperty.Create("Command", typeof(ICo

我正在创建一个特定于平台的自定义按钮,其中的文本和图像在Android和iOS中具有自定义渲染器。我的问题是,我的按钮上的“单击命令”不会被触发

我创建了自定义android渲染器,强制使用自定义android布局

Xamarin.Form按钮类:

    public static readonly BindableProperty CommandProperty =
        BindableProperty.Create("Command", typeof(ICommand), typeof(ImageButton), null,
            BindingMode.TwoWay, propertyChanged: OnCommandChanged);
    public ICommand Command
    {
        get { return (ICommand)GetValue(CommandProperty); }
        set { SetValue(CommandProperty, value); }
    }

    public EventHandler Clicked;

    private static void OnCommandChanged(BindableObject bindable, object oldValue, object newValue)
    {
        var control = (ImageButton)bindable;

        // this gesture recognizer will inovke the command event whereever it is used
        control.GestureRecognizers.Add(new TapGestureRecognizer
        {
            Command = (Command)newValue,
            CommandParameter = control.CommandParams
        });
    }        
    
    public ImageButton()
    {
        this.GestureRecognizers.Add(new TapGestureRecognizer
        {
            Command = new Command(() =>
            {
                Clicked?.Invoke(this, EventArgs.Empty);

                if (Command != null)
                {
                    if (Command.CanExecute(CommandParams))
                        Command.Execute(CommandParams);
                }
            })
        });
    }
Android渲染器:

[assembly: ExportRenderer(typeof(ImageButton), typeof(ImageButtonRenderer))]
    namespace Droid.Extensions.Renderers
    {
        public class ImageButtonRenderer : ViewRenderer<ImageButton, Android.Views.View>
        {
    private readonly Context context;
    private TextView buttonText;
    private ImageView buttonIcon;

    public ImageButtonRenderer(Context context) : base(context)
    {
        this.context = context;
    }

    protected override void OnElementChanged(ElementChangedEventArgs<Controls.ImageButton> e)
    {
        base.OnElementChanged(e);

        if (Control == null)
        {
            var inflater = context.GetSystemService(Context.LayoutInflaterService) as LayoutInflater;
            var rootLayout = inflater.Inflate(Resource.Layout.ImageButton, null, false);

            buttonText = (TextView) rootLayout.FindViewById(Resource.Id.image_button_text);
            buttonText.Text = Element.Text;
            buttonIcon = (ImageView)rootLayout.FindViewById(Resource.Id.image_button_icon);
            
                    
            SetNativeControl(rootLayout);
            rootLayout.Click += (s, a) => Element.Command?.Execute(a);
        }
    }

    protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        base.OnElementPropertyChanged(sender, e);
    }
        }
    }

我也有同样的问题。我试图通过BehaviorsPack绑定命令。它与原始元素完美配合。自定义控件不工作。我在xamarin表单中发布了一条帖子,但没有得到回复:

我能够找到代码中的问题。我正在将单击事件分配给不可单击的根布局。因此,我将单击指定给内部布局

SetNativeControl(rootLayout);
rootLayout.FindViewById(Resource.Id.image_button).Click += (s, a) =>
{
   Element.RaiseCommand();
};
此外,我们还必须将事件从本机控件冒泡到窗体

public void RaiseCommand()
{
    Clicked?.Invoke(this, EventArgs.Empty);

    if (Command != null && Command.CanExecute(CommandParams))
        Command.Execute(CommandParams);
}

谢谢分享。别忘了接受答案。
SetNativeControl(rootLayout);
rootLayout.FindViewById(Resource.Id.image_button).Click += (s, a) =>
{
   Element.RaiseCommand();
};
public void RaiseCommand()
{
    Clicked?.Invoke(this, EventArgs.Empty);

    if (Command != null && Command.CanExecute(CommandParams))
        Command.Execute(CommandParams);
}