I'm new to WPF and especially Commands, and I have task right now to build a RelayCommand for a button. I'm supposed to learn that I need to separate the logic from the UI. I just have 2 textboxes and a textBlock, the user writes the names in the boxes and clicks on a button to display them in the textblock. My task is to read about the RelayCommand and implement it, but I really don't understand how it works. I have an UpdateName method in my Logic.cs class, how do I use it in a RelayCommand? All I have is the RelayCommand.cs with the implemented ICommand Interface. This is the code I found online, but I really don't know what to put where.
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
private Action methodToExecute;
private Func<bool> canExecuteEvaluator;
public RelayCommand(Action methodToExecute, Func<bool> canExecuteEvaluator)
{
this.methodToExecute = 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.methodToExecute.Invoke();
}