RelayCommand

0

Czy RelayCommand w WPF dla mvvm powinny być dla każdego projektu osobno czy statycznie wspólne?

1

Możesz utworzyć klasę widoczną w całym projekcie:

   public class RelayCommand : ICommand, IDisposable
    {
        public event EventHandler CanExecuteChanged;

        private Func<bool> canExecuteFunc;
        private Action executeAction;
        private Action<object> execute;

        public RelayCommand(Action executeAction) : this(executeAction, () => true)
        {
        }

        public RelayCommand(Action executeAction, Func<bool> canExecuteFunc)
        {
            this.executeAction = executeAction;
            this.canExecuteFunc = canExecuteFunc;
        }

        public RelayCommand(Action<object> execute)
        {
            this.execute = execute;
        }

        public bool CanExecute(object parameter)
        {
            return canExecuteFunc();
        }

        public void Dispose()
        {
            RemoveAllEvents();
        }

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

        public void RemoveAllEvents()
        {
            CanExecuteChanged = null;
        }

        
    }

a widoku utworzyć obiekt tej klasy np.

private RelayCommand  commandDoSomething;

public RelayCommand CommandDoSomething => commandDoSomething ??
       (commandDoSomething = new RelayCommand(
       async () =>
       {
          await .....
       }
       ));

1 użytkowników online, w tym zalogowanych: 0, gości: 1