using System.Windows.Input; namespace LanMountainDesktop.Launcher.ViewModels; /// /// 简单的命令实现 /// public class RelayCommand : ICommand { private readonly Action _execute; private readonly Func? _canExecute; public RelayCommand(Action execute, Func? 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); } } /// /// 带参数的 RelayCommand /// public class RelayCommand : ICommand { private readonly Action _execute; private readonly Predicate? _canExecute; public RelayCommand(Action execute, Predicate? 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); } }