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