65 lines
2.0 KiB
C#
65 lines
2.0 KiB
C#
|
|
using System.Windows.Input;
|
|||
|
|
|
|||
|
|
namespace SHH.CameraDashboard;
|
|||
|
|
|
|||
|
|
// ===========================================================================
|
|||
|
|
// 1. 新增:非泛型 RelayCommand (支持 new RelayCommand(Method, Check))
|
|||
|
|
// ===========================================================================
|
|||
|
|
public class RelayCommand : ICommand
|
|||
|
|
{
|
|||
|
|
private readonly Action<object> _execute;
|
|||
|
|
private readonly Predicate<object> _canExecute;
|
|||
|
|
|
|||
|
|
public RelayCommand(Action<object> execute, Predicate<object> canExecute = null)
|
|||
|
|
{
|
|||
|
|
_execute = execute ?? throw new ArgumentNullException(nameof(execute));
|
|||
|
|
_canExecute = canExecute;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public bool CanExecute(object parameter)
|
|||
|
|
{
|
|||
|
|
return _canExecute == null || _canExecute(parameter);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void Execute(object parameter)
|
|||
|
|
{
|
|||
|
|
_execute(parameter);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public event EventHandler CanExecuteChanged
|
|||
|
|
{
|
|||
|
|
add => CommandManager.RequerySuggested += value;
|
|||
|
|
remove => CommandManager.RequerySuggested -= value;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// ===========================================================================
|
|||
|
|
// 2. 保留:泛型 RelayCommand<T> (支持 new RelayCommand<string>(Method...))
|
|||
|
|
// ===========================================================================
|
|||
|
|
public class RelayCommand<T> : ICommand
|
|||
|
|
{
|
|||
|
|
private readonly Action<T> _execute;
|
|||
|
|
private readonly Predicate<T> _canExecute;
|
|||
|
|
|
|||
|
|
public RelayCommand(Action<T> execute, Predicate<T> canExecute = null)
|
|||
|
|
{
|
|||
|
|
_execute = execute ?? throw new ArgumentNullException(nameof(execute));
|
|||
|
|
_canExecute = canExecute;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public bool CanExecute(object parameter)
|
|||
|
|
{
|
|||
|
|
return _canExecute == null || _canExecute((T)parameter);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void Execute(object parameter)
|
|||
|
|
{
|
|||
|
|
_execute((T)parameter);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public event EventHandler CanExecuteChanged
|
|||
|
|
{
|
|||
|
|
add => CommandManager.RequerySuggested += value;
|
|||
|
|
remove => CommandManager.RequerySuggested -= value;
|
|||
|
|
}
|
|||
|
|
}
|