Relay Command 的全面实施——是否适用于所有情况?

Full implementation of Relay Command - can it be applied to all cases?

我正在查看可以找到的中继命令的完整实现 here

我听说 RelayCommand 背后的想法是让一种“universal remote control”用于您的所有命令。

如果是这样,我有 2 个实施问题:

1) 如果我不想为某些控件传递参数,会发生什么情况?我一定要吗?我是否需要相应地更改我的 execute/can-execute 函数以支持这些参数?

2) 如果我不想在 XAML 中传递 CommandParameter 怎么办?如果我想通过使用 属性 更改或我的代码中的其他一些方法来影响控件的更改。我可以在不传递 XAML 中的 CommandParameter 的情况下影响 CanExecute 或 CanExecuteChanged 吗?

到目前为止,我主要实现了部分 RelayCommands,其中 CanExecute 始终返回 true,并且我只是将控件 IsEnabled 绑定到视图模型中的额外 属性。这工作得很好,但我想知道 - 完整的实施能为我做什么?

(能否给出一个完整的工作示例?)

1) ICommand 只有包含参数的方法。如果在 XAML 中没有指定参数,则使用 null。

https://msdn.microsoft.com/en-us/library/system.windows.input.icommand(v=vs.110).aspx

2) 是的,您可以在没有 CommandParameter 的情况下影响 CanExecute。见下文,它在 CanExecute.

中使用了视图模型的字符串 属性 "MyData"

MainWindow.xaml

<Window x:Class="WpfApplication8.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication8"
        mc:Ignorable="d"
        FocusManager.FocusedElement="{Binding ElementName=tb}"
        SizeToContent="Height"
        Title="MainWindow" Width="525">
    <Window.DataContext>
        <local:MainWindowViewModel />
    </Window.DataContext>
    <StackPanel>
        <Label Content="MyData (CanExecute returns false if this is whitespace)" />
        <TextBox Name="tb" Text="{Binding MyData, UpdateSourceTrigger=PropertyChanged}" Margin="5" />
        <Button Content="Without XAML CommandParameter" Margin="5" Command="{Binding Command1}" />
        <Button Content="With XAML CommandParameter" Margin="5" Command="{Binding Command1}" CommandParameter="{Binding MyData}" />
    </StackPanel>
</Window>

MainWindow.xaml.cs

using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Input;

namespace WpfApplication8
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }

    public class MainWindowViewModel : INotifyPropertyChanged
    {
        private ICommand command1;
        public ICommand Command1 { get { return command1; } set { command1 = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Command1))); } }

        private string myData;
        public string MyData { get { return myData; } set { myData = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(MyData))); } }

        public event PropertyChangedEventHandler PropertyChanged;

        public MainWindowViewModel()
        {
            Command1 = new RelayCommand<object>(Command1Execute, Command1CanExecute);
        }

        private bool Command1CanExecute(object obj)
        {
            // Only allow execute if MyData has data
            return !string.IsNullOrWhiteSpace(MyData);
        }

        private void Command1Execute(object obj)
        {
            MessageBox.Show($"CommandParameter = '{obj}'");
        }
    }

    public class RelayCommand<T> : ICommand
    {
        #region Fields

        readonly Action<T> _execute = null;
        readonly Predicate<T> _canExecute = null;

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of <see cref="DelegateCommand{T}"/>.
        /// </summary>
        /// <param name="execute">Delegate to execute when Execute is called on the command.  This can be null to just hook up a CanExecute delegate.</param>
        /// <remarks><seealso cref="CanExecute"/> will always return true.</remarks>
        public RelayCommand(Action<T> 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<T> execute, Predicate<T> canExecute)
        {
            if (execute == null)
                throw new ArgumentNullException("execute");

            _execute = execute;
            _canExecute = canExecute;
        }

        #endregion

        #region ICommand Members

        ///<summary>
        ///Defines the method that determines whether the command 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((T)parameter);
        }

        ///<summary>
        ///Occurs when changes occur that affect whether or not the command should execute.
        ///</summary>
        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }

        ///<summary>
        ///Defines the method to be called when the command is invoked.
        ///</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 <see langword="null" />.</param>
        public void Execute(object parameter)
        {
            _execute((T)parameter);
        }

        #endregion
    }
}