mirror of
https://github.com/PacktPublishing/Learn-WinUI-3-Second-Edition.git
synced 2026-06-20 12:23:09 +00:00
33 lines
889 B
C#
33 lines
889 B
C#
using System;
|
|
using System.Windows.Input;
|
|
|
|
namespace MyMediaCollection.ViewModels
|
|
{
|
|
public class RelayCommand : ICommand
|
|
{
|
|
private readonly Action action;
|
|
private readonly Func<bool> canExecute;
|
|
|
|
public RelayCommand(Action action)
|
|
: this(action, null)
|
|
{
|
|
}
|
|
|
|
public RelayCommand(Action action, Func<bool> canExecute)
|
|
{
|
|
if (action == null)
|
|
throw new ArgumentNullException(nameof(action));
|
|
|
|
this.action = action;
|
|
this.canExecute = canExecute;
|
|
}
|
|
|
|
public bool CanExecute(object parameter) => canExecute == null || canExecute();
|
|
|
|
public void Execute(object parameter) => action();
|
|
|
|
public event EventHandler CanExecuteChanged;
|
|
|
|
public void RaiseCanExecuteChanged() => CanExecuteChanged?.Invoke(this, EventArgs.Empty);
|
|
}
|
|
} |