Summary of three command s in MVVM mode -- DelegateCommand

After checking a lot of information, it is probably clear that there are three kinds. Of course, you can choose your own class name, but in order to facilitate memory and distinction, you still use the same class name as in the articles you see.

1.DelegateCommand

2.RelayCommand

3.AttachbehaviorCommand

Because MVVM mode is suitable for WPF and SL, there are some small differences among these three modes. For example, the CommandManager method under RelayCommand is under WPF and cannot be used under sl. however, I think the basic ideas of these three methods are the same, which are all from the article of the foreign ox man. The main difference lies in the binding and use of controls in VIEW. A little different, attachbhaviorcommand is a design pattern in prism 4, which is a little different. But I think the most convenient thing is this DelegateCommand.

        /// <summary>
      ///Delegatecommand and WPF.SL can be used. The trigger of INTERACTION is directly used in VIEW. It is reliable and suitable for different UIElement controls
      /// </summary>
     public class DelegateCommand : ICommand
      {
          Func<object, bool> canExecute;
          Action<object> executeAction;
          bool canExecuteCache;
  
         public DelegateCommand(Action<object> executeAction, Func<object, bool> canExecute)
         {
             this.executeAction = executeAction;
             this.canExecute = canExecute;
         }
 
         public bool CanExecute(object parameter)
         {
             bool temp = canExecute(parameter);
 
             if (canExecuteCache != temp)
             {
                 canExecuteCache = temp;
                 if (CanExecuteChanged != null)
                 {
                     CanExecuteChanged(this, new EventArgs());
                 }
             }

             return canExecuteCache;
         }

         public event EventHandler CanExecuteChanged;

         public void Execute(object parameter)
         {
                 executeAction(parameter);
         }

This class can be understood in this way. The action and func in the constructor are responsible for judging whether to execute the command. Action is the method to be executed after triggering the command. This is the easiest way to understand, but it is the most convenient for me who is just familiar with command to remember and learn. In order to use the methods and event interpretation implemented by ICommand interface, search can be found, but it is still a little obscure at the beginning.

The following is an example of using this command in VM. Bound a button control, the simplest example. cm1Click is the fuc in the constructor, which is responsible for executing the method of responding to events. Cancm1Click is the action in the constructor, which is responsible for judging whether the response event of this command is executed. There is no judgment formula here, and a true is directly assigned

public class TestViewModels:INotifyPropertyChanged
{
        public TestViewModels()
        {
            ......
            cm1click = new DelegateCommand(cm1Click,Cancm1Click);   //Initialize delegatecommand

        }
       ....

       //DelegateCommand

        #region command1

        public ICommand cm1click { get; set; }
        public void cm1Click(object param)
        {
            MessageBox.Show("CM1 clicked!");
        }

        private bool Cancm1Click(object param)
        {
            return true;
        }

        #endregion command1
       ......
}

In XAML, this event is bound with interaction instead of command in button. This has the advantage that it is very intuitive and can respond to many other events

        <Button x:Name="BTN_CM1" Content="DelegateCommand" Height="115" Width="148" >
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="Click">
                    <i:InvokeCommandAction Command="{Binding cm1click}"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </Button>

If other controls, such as grid, want to respond to selectionchanged events, you can directly modify the EventName in the trigger. It's more convenient in blend. You can choose directly. But there's a problem that I haven't figured out. In this case, how to obtain the event parameters? I have an example here. Take a look and update it.

Article source: Summary of three commands in MVVM mode [1] - delegatecommand - Security - blog Park

Tags: C# WPF

Posted on Tue, 21 Sep 2021 20:03:18 -0400 by munchy