RelayCommand parameter passing in Xamarin

后端 未结 2 1588
无人共我
无人共我 2021-01-14 05:12

I am very new to Xamarin cross-platform and while I did have some experience with WPF and MVVM I am still having issue understanding p

2条回答
  •  佛祖请我去吃肉
    2021-01-14 05:14

    Relay or Delegate Command for Xamarin

    That's how I achieve it, I hope it'll be helpful for someone

    public class DelegateCommand : ICommand
    {
        /// 
        /// The _execute
        /// 
        private readonly Action _execute;
    
        /// 
        /// The _can execute
        /// 
        private readonly Func _canExecute;
    
        /// 
        /// Initializes a new instance of the  class.
        /// 
        /// The execute.
        /// The can execute.
        /// execute
        public DelegateCommand(Action execute, Func canExecute)
        {
            _execute = execute ?? throw new ArgumentNullException("execute");
    
            if (canExecute != null)
            {
                this._canExecute = canExecute;
            }
        }
    
        /// 
        /// Initializes a new instance of the DelegateCommand class that
        /// can always execute.
        /// 
        /// The execution logic.
        /// If the execute argument is null.
        public DelegateCommand(Action execute)
            : this(execute, null)
        {
        }
    
        /// 
        /// Occurs when changes occur that affect whether the command should execute.
        /// 
        public event EventHandler CanExecuteChanged;
    
    
        /// 
        /// Raises the can execute changed.
        /// 
        public void RaiseCanExecuteChanged()
        {
    
            CanExecuteChanged?.Invoke(this, EventArgs.Empty);
        }
    
        /// 
        /// Defines the method that determines whether the command can execute in its current state.
        /// 
        /// Data used by the command.  If the command does not require data to be passed, this object can be set to null.
        /// true if this command can be executed; otherwise, false.
        public bool CanExecute(object parameter)
        {
            return _canExecute == null || _canExecute.Invoke();
        }
    
        /// 
        /// Defines the method to be called when the command is invoked.
        /// 
        /// Data used by the command.  If the command does not require data to be passed, this object can be set to null.
        public virtual void Execute(object parameter)
        {
            if (CanExecute(parameter))
            {
                _execute.Invoke();
            }
        }
    }
    
    /// 
    /// This class allows delegating the commanding logic to methods passed as parameters,
    /// and enables a View to bind commands to objects that are not part of the element tree.
    /// 
    /// Type of the parameter passed to the delegates
    public class DelegateCommand : ICommand
    {
        /// 
        /// The execute
        /// 
        private readonly Action _execute;
    
        /// 
        /// The can execute
        /// 
        private readonly Predicate _canExecute;
    
        /// 
        /// Initializes a new instance of the  class.
        /// 
        /// The execute action.
        /// execute
        public DelegateCommand(Action execute)
            : this(execute, null)
        {
        }
    
        /// 
        /// Initializes a new instance of the  class.
        /// 
        /// The execute.
        /// The can execute predicate.
        /// execute
        public DelegateCommand(Action execute, Predicate canExecute)
        {
            _execute = execute ?? throw new ArgumentNullException("execute");
    
            if (canExecute != null)
            {
                _canExecute = canExecute;
            }
        }
    
        /// 
        /// Occurs when changes occur that affect whether the command should execute.
        /// 
        public event EventHandler CanExecuteChanged;
    
        /// 
        /// Raise  event.
        /// 
        public void RaiseCanExecuteChanged()
        {
            CanExecuteChanged?.Invoke(this, EventArgs.Empty);
        }
    
        /// 
        /// Determines whether this instance can execute the specified parameter.
        /// 
        /// The parameter.
        /// true if this instance can execute the specified parameter; otherwise, false.
        public bool CanExecute(object parameter)
        {
            return _canExecute == null || _canExecute.Invoke((T)parameter);
        }
    
        /// 
        /// Executes the specified parameter.
        /// 
        /// The parameter.
        public virtual void Execute(object parameter)
        {
            if (CanExecute(parameter))
            {
                _execute((T)parameter);
            }
        }
    }
    

    in Your View

    in Your View Model

    public ICommand LoginCommand { get; }
    LoginCommand = new DelegateCommand(
    x =>
    {
         // x will be containing 12345
         // your code 
    });
    
        

    提交回复
    热议问题