First of all, the commands are part of the MVVM pattern and you should first read about it.
Interfaces in C # do not provide any functionality, they only describe how the class that inherits this interface should work. If you want the class to do something, you should not leave these methods empty.
Commands in WPF represent a kind of framework to which logic will be transmitted later. The most logical use of commands is to bind them to buttons.
ICommand implementation example:
public class RelayCommand : ICommand
{
private readonly Action<object> execute;
private readonly Func<object, bool> canExecute;
public event EventHandler CanExecuteChanged {
add => CommandManager.RequerySuggested += value;
remove => CommandManager.RequerySuggested -= value;
}
public RelayCommand(Action<object> execute, Func<object, bool> canExecute = null)
{
this.execute = execute;
this.canExecute = canExecute;
}
public bool CanExecute(object parameter)
{
return canExecute == null || canExecute(parameter);
}
public void Execute(object parameter)
{
execute(parameter);
}
}
Command using example:
public static RelayCommand NavigateToSignInPage => new RelayCommand(
actionParameter => Application.Instance.Navigation.NavigateTo(new LoginForm()));
public static RelayCommand NavigateToSignUpPage => new RelayCommand(
actionParameter => Application.Instance.Navigation.NavigateTo(new RegistrationForm()));
public static RelayCommand NavigateToStartPage => new RelayCommand(
actionParameter => Application.Instance.Navigation.NavigateTo(new StartPage()));
public static RelayCommand NavigateBack => new RelayCommand(
actionParameter => Application.Instance.Navigation.NavigateBack(),
actionPossibilityParameter => Application.Instance.Navigation.BackNavigationPossible);
Command binding example:
In View (xaml):
<Button x:Name="CancelButton"
Content="Cancel"
Command="{Binding CancelCommand}"
Grid.Row="2"
IsCancel="True"
HorizontalAlignment="Left"
Margin="44,0,0,0"
Width="118" Height="23"
VerticalAlignment="Center" />
In ViewModel:
public RelayCommand CancelCommand => NavigationCommands.NavigateBack;