123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- using System;
- using System.Windows.Input;
- namespace Flyinsono.Client.Test
- {
- /// <summary>
- /// Implementation of ICommand
- /// </summary>
- public abstract class Command : ViewModel, ICommand
- {
- private readonly Action<object> _executeAction;
- private readonly Func<object, bool> _canExecuteFunc;
- public event EventHandler CanExecuteChanged;
- protected Command(Action<object> executeAction, Func<object, bool> canExecuteFunc = null)
- {
- if (executeAction == null)
- {
- throw new NullReferenceException("Execute Action can not be null");
- }
- _executeAction = executeAction;
- _canExecuteFunc = canExecuteFunc;
- }
- /// <summary>
- /// If this command can executed
- /// </summary>
- /// <param name="parameter"></param>
- /// <returns></returns>
- public bool CanExecute(object parameter)
- {
- if (!IsEnabled)
- {
- return false;
- }
- if (_canExecuteFunc != null)
- {
- return _canExecuteFunc(parameter);
- }
- return true;
- }
- /// <summary>
- /// Execute the command
- /// </summary>
- /// <param name="parameter"></param>
- public void Execute(object parameter)
- {
- if (CanExecute(parameter))
- {
- Logger.WriteLineInfo($"Execute Command with description {Description}");
- _executeAction(parameter);
- }
- }
- public virtual void OnCanExecuteChanged()
- {
- CanExecuteChanged?.Invoke(this, EventArgs.Empty);
- }
- protected override void OnIsEnabledChanged()
- {
- OnCanExecuteChanged();
- base.OnIsEnabledChanged();
- }
- }
- }
|