在Xamarin中传递的RelayCommand参数

eYe*_*eYe 3 c# wpf mvvm xamarin

我是很新的Xamarin cross-platform,虽然我也有一定的经验WPFMVVM我仍然有问题,了解参数RelayCommand使用调用ICommand下面执行.有人可以解释如何正确地传递和接收CommandParameter我的视图到我的界限,RelayCommand因为这似乎安静不同于正常WPF版本RelayCommand:

    /// <summary>
    /// A command whose sole purpose is to relay its functionality 
    /// to other objects by invoking delegates. 
    /// The default return value for the CanExecute method is 'true'.
    /// <see cref="RaiseCanExecuteChanged"/> needs to be called whenever
    /// <see cref="CanExecute"/> is expected to return a different value.
    /// </summary>
    public class RelayCommand : ICommand
    {
        private readonly Action _execute;
        private readonly Func<bool> _canExecute;

        /// <summary>
        /// Raised when RaiseCanExecuteChanged is called.
        /// </summary>
        public event EventHandler CanExecuteChanged;

        /// <summary>
        /// Creates a new command that can always execute.
        /// </summary>
        /// <param name="execute">The execution logic.</param>
        public RelayCommand(Action execute)
            : this(execute, null)
        {
        }

        /// <summary>
        /// Creates a new command.
        /// </summary>
        /// <param name="execute">The execution logic.</param>
        /// <param name="canExecute">The execution status logic.</param>
        public RelayCommand(Action execute, Func<bool> canExecute)
        {
            if (execute == null)
                throw new ArgumentNullException("execute");
            _execute = execute;
            _canExecute = canExecute;
        }

        /// <summary>
        /// Determines whether this <see cref="RelayCommand"/> can execute in its current state.
        /// </summary>
        /// <param name="parameter">
        /// Data used by the command. If the command does not require data to be passed, this object can be set to null.
        /// </param>
        /// <returns>true if this command can be executed; otherwise, false.</returns>
        public bool CanExecute(object parameter)
        {
            return _canExecute == null ? true : _canExecute();
        }

        /// <summary>
        /// Executes the <see cref="RelayCommand"/> on the current command target.
        /// </summary>
        /// <param name="parameter">
        /// Data used by the command. If the command does not require data to be passed, this object can be set to null.
        /// </param>
        public void Execute(object parameter)
        {
            _execute();
        }

        /// <summary>
        /// Method used to raise the <see cref="CanExecuteChanged"/> event
        /// to indicate that the return value of the <see cref="CanExecute"/>
        /// method has changed.
        /// </summary>
        public void RaiseCanExecuteChanged()
        {
            var handler = CanExecuteChanged;
            if (handler != null)
            {
                handler(this, EventArgs.Empty);
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

在WPF之前,我曾经有类似的东西:

<Command="{Binding OpenMenuItemCommand}"
            CommandParameter="{Binding SelectedItem}"/>
Run Code Online (Sandbox Code Playgroud)

并在ViewModel旁边:

  OpenMenuItemCommand = new RelayCommand(OpenMenuItem);
  ...
  public void OpenMenuItem(object sender, ItemTappedEventArgs args)
  {
  }
Run Code Online (Sandbox Code Playgroud)

所以我的参数会通过args.

And*_*tro 5

我相信你会让事件和命令混乱.两者之间的一些区别是您需要订阅事件并且必须发生事件.命令可以被任何人调用,也可以被阻止.

So to get you example to work correctly you should modify your code to allow your RelayCommand to take an action with a parameter. This parameter will define the Type of the parameter. I would use something like MVVMLight which contains a Generic RelayCommand so that you don't have to write your own. Once that is done you should be able to change your code to look like this.

 OpenMenuItemCommand = new RelayCommand<MenuItem>(OpenMenuItem);
  ...
  public void OpenMenuItem(MenuItem item)
  {
  }
Run Code Online (Sandbox Code Playgroud)

I wrote a small blog post that contains a full working project if you want to see a working example.