New to C#/MVVM and this doesn't make sense to me?
This is my implementation of a RelayCommand inheriting from ICommand:
internal class RelayCommand : ICommand
{
private readonly Predicate<object> _canExecute;
private readonly Action _execute;
public event EventHandler CanExecuteChanged = (sender, e) => {};
public RelayCommand(Action execute) : this(execute, null){ }
public RelayCommand(Action execute, Predicate<object> canExecute)
{
_execute = execute;
_canExecute = canExecute;
}
public bool CanExecute(object parameter) => (_canExecute == null) ? true : _canExecute(parameter);
public void Execute(object parameter) => _execute();
}
I found out through testing that I simply can't just do this:
public RelayCommand TestCommand;
I have to do this:
public RelayCommand TestCommand { get; set; }
Otherwise declaring the command in the constructor like this:
TestCommand = new RelayCommand(TestCommandFunction);
public void TestCommandFunction(){}
won't work. Why is this the case?