MVVM ICommand explanation ...

JerryM

Member
Joined
May 13, 2021
Messages
10
Programming Experience
Beginner
Hi,
here is my short code in MS VS 2019 C# WPF - MVVM, can someone please explain me:
1/ where is pointed my code to after "return _SaveCommand;" which is placed to "public ICommand SaveCommand" property ? It is classic MVVM.
2/ And who is caller of "CommandManager.RequerySuggested += value;" in "add event" in "public class RelayCommand : ICommand" ?? What is "value" ???
...there are some invisible parts of code added by MS VS in backgroud ... and I do not understand to it ...
thanks J.

if someone need source code: WpfApp024 relay command v konstruktoru.zip

C#:
public class VM
    {
        public String Name { get; set; }

        // classic Button Save described in XAML as:  <Button Content="Save" Command="{Binding SaveCommand}" Margin="3" />
        private ICommand _SaveCommand;

        public ICommand SaveCommand
        {
            get { return _SaveCommand;  }
        }

        public VM()
        {
            _SaveCommand = new RelayCommand(SaveCommand_Execute, SaveCommand_CanExecute);
        }

        public void SaveCommand_Execute()
        {
            MessageBox.Show("Save called");
        }

        public bool SaveCommand_CanExecute()
        {
            //MessageBox.Show("Save called");

            if ( string.IsNullOrEmpty(Name))
            {
                return false;
            }
            else
            {
                return true;
            }

        }

    }

    public class RelayCommand : ICommand
    {
        private Action methodToxecute;
        private Func<bool> canExecuteEvaluator;

        public event EventHandler CanExecuteChanged
        {
            add
            {
                CommandManager.RequerySuggested += value;
                //MessageBox.Show(value.ToString());

            }
            remove { CommandManager.RequerySuggested -= value; }
        }

        public RelayCommand(Action methodToExecute, Func<bool> canExecuteEvaluator)
        {
            this.methodToxecute = methodToExecute;
            this.canExecuteEvaluator = canExecuteEvaluator;
        }

        public RelayCommand(Action methodToExecute) : this(methodToExecute, null)
        {

        }

        public bool CanExecute(object parameter)
        {
            if ( this.canExecuteEvaluator == null)
            {
                return true;
            }
            else
            {
                bool result = this.canExecuteEvaluator.Invoke();
                return result;
            }
        }

        public void Execute(object parameter)
        {
            this.methodToxecute.Invoke();
        }

    }
 
1/ where is pointed my code to after "return _SaveCommand;" which is placed to "public ICommand SaveCommand" property ?
Code execution will return to the code generated by code generated by XAML compiler when it sees the Command binding within your XAML. If you are not using declarative XAML, and instead using imperative coding with WPF, it will be where ever you wrote code to mind the Command property of the WPF control.
 
What is "value" ???
C# setters and adders have an implied value parameter. It is whatever the caller is passing in to the property setter, or event adder.
 
2/ And who is caller of "CommandManager.RequerySuggested += value;" in "add event" in "public class RelayCommand : ICommand" ??
It will be the WPF framework code which will register for an event notification.
 
Back
Top Bottom