namespace VideoBrowser.Common
{
using System;
using System.Windows.Input;
///
/// The WPF default command.
///
///
public class RelayCommand : ICommand
{
private readonly Func _canExecute;
private readonly Action _execute;
private readonly string _name;
///
/// Initializes a new instance of the class.
///
/// The execute.
/// The can execute.
public RelayCommand(Action execute, string name = "", Func canExecute = null)
{
this._execute = execute;
this._name = name;
this._canExecute = canExecute;
}
///
/// Occurs when changes occur that affect whether or not the command should execute.
///
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
///
/// Defines the method that determines whether the command can execute in its current state.
///
/// Data used by the command. If the command does not require data to be passed, this object can be set to .
///
/// if this command can be executed; otherwise, .
///
public bool CanExecute(object parameter)
{
return this._canExecute == null || this._canExecute(parameter);
}
///
/// Defines the method to be called when the command is invoked.
///
/// Data used by the command. If the command does not require data to be passed, this object can be set to .
public void Execute(object parameter)
{
this._execute(parameter);
Logger.Info($"{nameof(RelayCommand)} {this._name} is executed");
}
}
}