mirror of
https://github.com/wwiinnddyy/LanMountainDesktop.git
synced 2026-06-20 23:54:26 +08:00
68 lines
1.5 KiB
C#
68 lines
1.5 KiB
C#
using System.Windows.Input;
|
|
|
|
namespace LanMountainDesktop.Launcher.ViewModels;
|
|
|
|
/// <summary>
|
|
/// 简单的命令实现
|
|
/// </summary>
|
|
public class RelayCommand : ICommand
|
|
{
|
|
private readonly Action _execute;
|
|
private readonly Func<bool>? _canExecute;
|
|
|
|
public RelayCommand(Action execute, Func<bool>? canExecute = null)
|
|
{
|
|
_execute = execute ?? throw new ArgumentNullException(nameof(execute));
|
|
_canExecute = canExecute;
|
|
}
|
|
|
|
public bool CanExecute(object? parameter)
|
|
{
|
|
return _canExecute?.Invoke() ?? true;
|
|
}
|
|
|
|
public void Execute(object? parameter)
|
|
{
|
|
_execute();
|
|
}
|
|
|
|
public event EventHandler? CanExecuteChanged;
|
|
|
|
public void RaiseCanExecuteChanged()
|
|
{
|
|
CanExecuteChanged?.Invoke(this, EventArgs.Empty);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 带参数的 RelayCommand
|
|
/// </summary>
|
|
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?.Invoke((T)parameter!) ?? true;
|
|
}
|
|
|
|
public void Execute(object? parameter)
|
|
{
|
|
_execute((T)parameter!);
|
|
}
|
|
|
|
public event EventHandler? CanExecuteChanged;
|
|
|
|
public void RaiseCanExecuteChanged()
|
|
{
|
|
CanExecuteChanged?.Invoke(this, EventArgs.Empty);
|
|
}
|
|
}
|