Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace "MvvmLight..." with "RelayCommand" #59

Open
singba opened this issue Jan 9, 2020 · 0 comments
Open

replace "MvvmLight..." with "RelayCommand" #59

singba opened this issue Jan 9, 2020 · 0 comments

Comments

@singba
Copy link

singba commented Jan 9, 2020

using System;
using System.Windows.Input;

namespace ConfuserEx
{
    public class RelayCommand : RelayCommand<object>, ICommand
    { 
        public RelayCommand(Action execute)
            : base((p) => execute())
        {
        }

        public RelayCommand(Action execute, Func<bool> canExecute)
            :base((p)=>execute(),(p)=>canExecute())
        { 
        }
    }

    public class RelayCommand<T> : ICommand
    {
        #region Fields

        private Func<T, bool> _canExecute;
        private Action<T> _execute;
        private bool _IsExecuting = false;
        #endregion // Fields

        #region Constructors 
        public RelayCommand(Action<T> execute)
            : this(execute, null)
        {
        }

        public RelayCommand(Action<T> execute, Func<T, bool> canExecute)
        {
            ResetActions(execute, canExecute);
        } 
        #endregion // Constructors

        #region ICommand Members 
        public bool CanExecute(object parameter)
        {
            return _canExecute == null ? true : ((!_IsExecuting) && _canExecute((T)parameter));
        }

        public event EventHandler CanExecuteChanged
        {
            add { System.Windows.Input.CommandManager.RequerySuggested += value; }
            remove { System.Windows.Input.CommandManager.RequerySuggested -= value; }
        }

        public void Execute(object parameter)
        {
            try
            {
                if (_IsExecuting)
                    return;
                _IsExecuting = true;
                RaiseCanExecuteChanged();
                _execute((T)parameter);
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                _IsExecuting = false;
                RaiseCanExecuteChanged();
            }
        }
        #endregion

        public void ResetActions(Action<T> execute, Func<T, bool> canExecute)
        {
            if (execute != null)
            {
                _execute = execute;
            }
            if (canExecute != null)
            {
                _canExecute = canExecute;
            }
        }

        public void RaiseCanExecuteChanged()
        {
            System.Windows.Input.CommandManager.InvalidateRequerySuggested();
        } 
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant