WPF wpf operating authority control buttons

Original: WPF WPF control buttons operating authority

Access control there are many ways we can achieve.

The project to be a simple access control, before we judge all buttons trigger, you can have permission to perform.

We order a custom class.

 public class DelegateCommand : ICommand
    {
        Action _action;
        Func<bool> _canAction;
        string _Role;
        public DelegateCommand(Action action,string Role=null)
        {
            _Role = Role;
            _action = action;
        }

        public DelegateCommand(Action action, Func<bool> canAction, string Role = null)
        {
            _Role = Role;
            _action = action;
            _canAction = canAction;
        }

        public bool CanExecute(object parameter)
        {
            if (_canAction == null)
            {
                return true;
            }
            return _canAction.Invoke();
        }

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

        public void Execute(object parameter)
        {
            if (_action != null)
            {
                if (_Role!=null)
                {
                   if ( System.Windows.MessageBox.Show("此权限为"+_Role,"",System.Windows.MessageBoxButton.OKCancel)!= System.Windows.MessageBoxResult.OK)
                    {
                        return;
                    }
                }
                _action.Invoke();
            }
        }

Verify permissions in the Execute

The rights object with this button when incoming Command

AbnormalCommand = new DelegateCommand<string>((p) => Messenger.Default.Send<string>(p, "HomeNavgation"),"Abnormal Button Role");

 

Guess you like

Origin www.cnblogs.com/lonelyxmas/p/11112123.html