using System.ComponentModel;
using System.Windows.Input;
/// <summary>
/// Commandパターンのサンプルコード
/// </summary>
public class Command : ICommand
{
Action _action;
public Command(Action action)
{
_action = action;
}
public event EventHandler? CanExecuteChanged;
public bool CanExecute(object? parameter) => true;
public void Execute(object? parameter)
{
_action.Invoke();
}
}
public class ViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler? PropertyChanged;
protected virtual void OnPropertyChanged(string name) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
public Command? MyCommand { get; }
public ViewModel()
{
MyCommand = new Command(()=>
{
Console.WriteLine("Call MyCommand!!");
});
}
}
static class Program
{
static public void Main()
{
var vm = new ViewModel();
vm.MyCommand?.Execute(null);
// Call MyCommand!!
}
}
コメント