zoukankan      html  css  js  c++  java
  • WPF wpf中按钮操作权限控制

    权限控制我们有很多种方式可以实现。

    这次项目中做个简单的权限控制,我们在所有按钮触发前判断,有权限则可执行。

    我们自定义一个命令类。

     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();
                }
            }

    在Execute时验证权限

    用的时候将此按钮的权限对象传入Command

    AbnormalCommand = new DelegateCommand<string>((p) => Messenger.Default.Send<string>(p, "HomeNavgation"),"Abnormal Button Role");
  • 相关阅读:
    My Eclipse
    那一夜,我被梦中笑醒的事之(数据库)
    KTV项目总结
    欢迎来到,数据库联盟!
    学习手工创建表,表关系以及用exists 来查询
    sql 将Null 值转化成空字符串
    jquery toggle
    推荐一个不错的配色网站
    css之zindex
    关于前后端分离与不分离
  • 原文地址:https://www.cnblogs.com/czly/p/10718328.html
Copyright © 2011-2022 走看看