• Command


    参考JusterZhu视频和文档,ppt文档基本全抄

    Command

    日常开发中必不可少会用到命令,比如button自带了Command和CommandParameter属性。细心的小伙可能会发现并不是所有的控件都自带这样的属性,那么如何让“万物皆可Command”呢?

    1. 通常使用System.Windows.Interactivity.dll提供的Interaction对象,它的本质是附加属性可以附加到wpf所有的控件之上,因为所有wpf控件的基类都是DependencyObject的。

    2. 避免版本问题,推荐使用Microsoft.Xaml.Behaviors.Wpf 该组件支持.net5
      https://github.com/Microsoft/XamlBehaviorsWpf

    如何使用Interactivity 对象?

    1. Install-Package Microsoft.Xaml.Behaviors.Wpf -Version 1.1.31;
    2. XAML文件中引入 xmlns:i=http://schemas.microsoft.com/xaml/behaviors;
    3. 任意控件标签中添加Interaction.Triggers;
    4. 指定EventTrigger的名称,每个控件都会自带一些事件填写该名称即可;
    5. 指定InvokeCommandAction最终需要执行的命令;
    
        
        
        
            
                
            
        
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    internal class MainViewModel
    {
        public ICommand TestCommand { get; set; }
        public MainViewModel() 
        {
            TestCommand = new RelayCommand(TestCallBack);
        }
    
        private void TestCallBack()
        {
            Application.Current.Dispatcher.Invoke(() => 
            {
                MessageBox.Show("hello world.");
            });
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    public class RelayCommand : ICommand
    {
        #region 字段
        readonly Func _canExecute;
        readonly Action _execute;
        #endregion
    
        #region 构造函数
        public RelayCommand(Action execute) : this(execute, null)
        {
            
        }
    
        public RelayCommand(Action execute, Func canExecute)
        {
            if (execute == null)
            {
                throw new ArgumentNullException("execute");
            }
            _execute = execute;
            _canExecute = canExecute;
        }
        #endregion
    
        #region ICommand的成员
    
        public event EventHandler CanExecuteChanged
        {
            add
            {
                if (_canExecute != null)
                    CommandManager.RequerySuggested += value;
            }
    
            remove
            {
                if (_canExecute != null)
                    CommandManager.RequerySuggested -= value;
            }
        }
    
        [DebuggerStepThrough]
    
        public Boolean CanExecute(Object parameter)
        {
            return _canExecute == null ? true : _canExecute();
        }
    
        public void Execute(Object parameter)
        {
            _execute();
        }
    
        #endregion
    }
    
    /// 
    /// A command whose sole purpose is to 
    /// relay its functionality to other
    /// objects by invoking delegates. The
    /// default return value for the CanExecute
    /// method is 'true'.
    /// 
    public class RelayCommand : ICommand
    {
        #region Constructors
    
        public RelayCommand(Action execute)
            : this(execute, null)
        {
        }
    
        /// 
        /// Creates a new command.
        /// 
        /// The execution logic.
        /// The execution status logic.
        public RelayCommand(Action execute, Predicate canExecute)
        {
            if (execute == null)
                throw new ArgumentNullException("execute");
    
            _execute = execute;
            _canExecute = canExecute;
        }
    
        #endregion // Constructors
    
        #region ICommand Members
    
        [DebuggerStepThrough]
        public bool CanExecute(object parameter)
        {
            return _canExecute == null || _canExecute((T)parameter);
        }
    
        public event EventHandler CanExecuteChanged
        {
            add
            {
                if (_canExecute != null)
                    CommandManager.RequerySuggested += value;
            }
            remove
            {
                if (_canExecute != null)
                    CommandManager.RequerySuggested -= value;
            }
        }
    
        public void Execute(object parameter)
        {
            _execute((T)parameter);
        }
    
        #endregion // ICommand Members
    
        #region Fields
    
        readonly Action _execute = null;
        readonly Predicate _canExecute = null;
    
        #endregion // Fields
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
  • 相关阅读:
    你需要知道的ES6—ES13开发技巧
    卷积神经网络的常用改进
    数字信号处理学习笔记(一):离散时间信号与系统
    单片机IAP固件升级分几步?(Qt上位机)
    多帧三角化原理
    LogCat工具
    OCR测试—文字密度和中英文
    《持续交付:发布可靠软件的系统方法》- 读书笔记(三)
    springboot-iconfont图标如何使用?
    Java - NPE(NullPointerException);Optional
  • 原文地址:https://blog.csdn.net/zzyzxb/article/details/134062582