阅读 108

WPF MVVM模式开发实现简明教程 3-1 BaseCommand

namespace WpfApp6.ViewModel
{
    using System;
    using System.Windows.Input;

    /// 
    /// 命令基类
    /// 
    public class BaseCommand : ICommand
    {
        public event EventHandler CanExecuteChanged
        {
            add
            {
                if (_canExecute != null)
                {
                    CommandManager.RequerySuggested += value;
                }
            }
            remove
            {
                if (_canExecute != null)
                {
                    CommandManager.RequerySuggested -= value;
                }
            }
        }

        public bool CanExecute(object parameter)
        {
            if (_canExecute == null)
            {
                return true;
            }
            return _canExecute(parameter);
        }

        public void Execute(object parameter)
        {
            if (_execute != null && CanExecute(parameter))
            {
                _execute(parameter);
            }
        }

        private Func _canExecute;
        private Action _execute;

        public BaseCommand(Action execute, Func canExecute)
        {
            _execute = execute;
            _canExecute = canExecute;
        }

        public BaseCommand(Action execute) :
            this(execute, null)
        {
        }
    }
}


  网上的代码,主要是Execute,CanExecute,不过不需要理解的,知道需要这个东西就行

原文:https://www.cnblogs.com/jhlong/p/14148911.html

文章分类
代码人生
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐