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

前端 未结 1 635
梦谈多话
梦谈多话 2021-01-25 21:41

I\'m looking at the full implementation of a Relay Command that can be found here

I heard that the idea behind RelayCommand is to have a sort of \"universal remote contr

1条回答
  •  醉话见心
    2021-01-25 22:14

    1) ICommand only has methods that include a parameter. If you don't specify a parameter in XAML, null is used.

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

    2) Yes, you can effect CanExecute without a CommandParameter. See below, it uses the viewmodel's string property "MyData" in CanExecute.

    MainWindow.xaml

    
        
            
        
        
            

    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(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 : ICommand
        {
            #region Fields
    
            readonly Action _execute = null;
            readonly Predicate _canExecute = null;
    
            #endregion
    
            #region Constructors
    
            /// 
            /// Initializes a new instance of .
            /// 
            /// Delegate to execute when Execute is called on the command.  This can be null to just hook up a CanExecute delegate.
            ///  will always return true.
            public RelayCommand(Action execute)
                : this(execute, null)
            {
            }
    
            /// 
            /// Creates a new command.
            /// 
            /// The execution logic.
            /// The execution status logic.
            public RelayCommand(Action execute, Predicate canExecute)
            {
                if (execute == null)
                    throw new ArgumentNullException("execute");
    
                _execute = execute;
                _canExecute = canExecute;
            }
    
            #endregion
    
            #region ICommand Members
    
            ///
            ///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 ? true : _canExecute((T)parameter);
            }
    
            ///
            ///Occurs when changes occur that affect whether or not the command should execute.
            ///
            public event EventHandler CanExecuteChanged
            {
                add { CommandManager.RequerySuggested += value; }
                remove { CommandManager.RequerySuggested -= value; }
            }
    
            ///
            ///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 .
            public void Execute(object parameter)
            {
                _execute((T)parameter);
            }
    
            #endregion
        }
    }
    
        

    0 讨论(0)
    提交回复
    热议问题