Delegate Command
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
namespace MyApp.ViewModel
{
// Command delegate implementation with no parameters
public class DelegateCommand : ICommand
{
private readonly Action _action;
public DelegateCommand(Action action)
{
_action = action;
}
public void Execute(object parameter)
{
_action.Invoke();
}
public bool CanExecute(object parameter)
{
return true;
}
#pragma warning disable 67
public event EventHandler CanExecuteChanged;
#pragma warning restore 67
}
// Command delegate implementation with parameters
public class DelegateCommand<T> : ICommand
{
private readonly Action<T> _action;
public DelegateCommand(Action<T> action)
{
_action = action;
}
public void Execute(object parameter)
{
_action((T)Convert.ChangeType(parameter, typeof(T)));
}
public bool CanExecute(object parameter)
{
return true;
}
#pragma warning disable 67
public event EventHandler CanExecuteChanged;
#pragma warning restore 67
}
}