【问题标题】:How to create bindable command for custom component in Xamarin.Forms?如何在 Xamarin.Forms 中为自定义组件创建可绑定命令?
【发布时间】:2020-06-13 19:03:42
【问题描述】:

我有一个自定义组件,其中有按钮,我正在尝试创建一个可绑定的命令,以便我可以根据视图模型执行操作。 我尝试了几件事,但似乎没有任何效果:

public static readonly BindableProperty CommandProperty = 
    BindableProperty.Create(nameof(Command), typeof(ICommand), typeof(MySample), null);

public ICommand Command
{
    get { return (ICommand)GetValue(CommandProperty); }
    set { SetValue(CommandProperty, value); }
}

// Helper method for invoking commands safely
public static void Execute(ICommand command)
{
    if (command == null) return;
    if (command.CanExecute(null))
    {
        command.Execute(null);
    }
}

【问题讨论】:

    标签: c# xamarin xamarin.forms


    【解决方案1】:

    需要使用TapGestureRecognizer触发Command

    public partial class View1 : ContentView
    {
        public View1()
        {
            InitializeComponent();
    
            var gestureRecognizer = new TapGestureRecognizer();
    
            gestureRecognizer.Tapped += (s, e) => {
                if (Command != null && Command.CanExecute(null))
                {
                    Command.Execute(null);
                }
            };
    
            this.GestureRecognizers.Add(gestureRecognizer);
        }
        // BindableProperty implementation
        public static readonly BindableProperty CommandProperty =
            BindableProperty.Create(nameof(Command), typeof(ICommand), typeof(View1), null);
    
        public ICommand Command
        {
            get { return (ICommand)GetValue(CommandProperty); }
            set { SetValue(CommandProperty, value); }
        }
    
        // Helper method for invoking commands safely
        public static void Execute(ICommand command)
        {
            if (command == null) return;
            if (command.CanExecute(null))
            {
                command.Execute(null);
            }
        }
    }
    

    我上传了一个示例项目here,欢迎随时问我任何问题。

    【讨论】:

    • 它更像是在整个视图而不是特定元素上创建行为。
    【解决方案2】:

    你需要implement propertychanged for bindable property to enable binding.

    public static readonly BindableProperty CommandProperty = 
        BindableProperty.Create(nameof(Command), typeof(Xamarin.Forms.Command), typeof(MySample), null, propertychanged: OnCommandPropertyChanged);
    
    stativ void OnCommandPropertyChanged  (BindableObject bindable, object oldValue, object newValue)
    {
        (bindable as MySample).Command = (Command)newValue;
    }
    

    【讨论】:

    • 试了试了!!绑定命令,如:Command={Binding NextCommand} on xaml 应用 propertyChanged 事件后。
    • 太棒了。如果这解决了您的问题,请点赞并接受。这将帮助其他有相同问题的用户。
    • 嘿!我已经尝试过相同的条目,但它不起作用。我也添加了属性更改事件。
    【解决方案3】:

    public ICommand MyCommand
            {
                get => (ICommand)GetValue(MyCommandProperty);
                set => SetValue(MyCommandProperty, value);
            }
    
            public static BindableProperty MyCommandProperty = BindableProperty.Create(
                                                     propertyName: "Command",
                                                     returnType: typeof(ICommand),
                                                     declaringType: typeof(View1),
                                                     defaultValue: null,
                                                     defaultBindingMode: BindingMode.TwoWay,
                                                     propertyChanged: MyCommandPropertyChanged);
    
            public static void MyCommandPropertyChanged(BindableObject bindable, object oldValue, object newValue)
            {
                var control = (CustomSignInTemplate)bindable;
                control.tempbtn.Command = (ICommand)newValue;
            }

    【讨论】:

      猜你喜欢
      • 2011-12-01
      • 1970-01-01
      • 2017-03-11
      • 2019-03-09
      • 1970-01-01
      • 2022-01-16
      • 1970-01-01
      • 2018-07-26
      • 2012-02-09
      相关资源
      最近更新 更多