command pattern
This commit is contained in:
39
PacticeSolution/CommandPatternSample/Command/RelayCommand.cs
Normal file
39
PacticeSolution/CommandPatternSample/Command/RelayCommand.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Input;
|
||||
|
||||
namespace CommandPatternSample.Command
|
||||
{
|
||||
internal class RelayCommand : ICommand
|
||||
{
|
||||
private Func<object, bool> _canExecute;
|
||||
private Action<object> _executeAction;
|
||||
|
||||
public event EventHandler CanExecuteChanged;
|
||||
|
||||
public RelayCommand(Action<object> executeAction, Func<object, bool> canExeute)
|
||||
{
|
||||
_executeAction = executeAction ?? throw new ArgumentNullException("Execute action is not null");
|
||||
_canExecute = canExeute;
|
||||
}
|
||||
|
||||
public RelayCommand(Action<object> executeAction) : this(executeAction, null)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public bool CanExecute(object parameter)
|
||||
{
|
||||
bool result = _canExecute == null ? true : _canExecute.Invoke(parameter);
|
||||
return result;
|
||||
}
|
||||
|
||||
public void Execute(object parameter)
|
||||
{
|
||||
_executeAction.Invoke(parameter);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user