Create an ICommand Bindable property on Xamarin Forms

前端 未结 2 1055
滥情空心
滥情空心 2021-02-13 16:13

I have a custom checkbox control that I created with an ICommand property and the corresponding bindable property (my checkbox is a Xamarin.Forms XAML Page), the code is:

<
2条回答
  •  迷失自我
    2021-02-13 16:52

    Something like that (pseudocode):

    public class YourClassName : View
    {
        public YourClassName()
        {
            var gestureRecognizer = new TapGestureRecognizer();
    
            gestureRecognizer.Tapped += (s, e) => {
                if (Command != null && Command.CanExecute(null)) {
                    Command.Execute(null);
                }
            };
    
            var label = new Label();
            label.GestureRecognizers.Add(gestureRecognizer);
        }
    
        public static readonly BindableProperty CommandProperty =
        BindableProperty.Create(x => x.Command, null);
    
        public ICommand Command
        {
            get { return (ICommand)GetValue(CommandProperty); }
            set { SetValue(CommandProperty, value); }
        }
    }
    

提交回复
热议问题