zoukankan      html  css  js  c++  java
  • DelegateCommand.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Input;

    namespace SC
    {
        /// <summary>
        /// delegate command for view model
        /// </summary>
        public class DelegateCommand : ICommand
        {
            #region members
            /// <summary>
            /// can execute function
            /// </summary>
            private readonly Func<bool> canExecute;

            /// <summary>
            /// execute function
            /// </summary>
            private readonly Action execute;

            #endregion

            /// <summary>
            /// Initializes a new instance of the DelegateCommand class.
            /// </summary>
            /// <param name="execute">indicate an execute function</param>
            public DelegateCommand(Action execute)
                : this(execute, null)
            {
            }

            /// <summary>
            /// Initializes a new instance of the DelegateCommand class.
            /// </summary>
            /// <param name="execute">execute function </param>
            /// <param name="canExecute">can execute function</param>
            public DelegateCommand(Action execute, Func<bool> canExecute)
            {
                this.execute = execute;
                this.canExecute = canExecute;
            }

            /// <summary>
            /// can executes event handler
            /// </summary>
            public event EventHandler CanExecuteChanged
            {
                add { CommandManager.RequerySuggested += value; }
                remove { CommandManager.RequerySuggested -= value; }
            }

            /// <summary>
            /// implement of icommand can execute method
            /// </summary>
            /// <param name="parameter">parameter by default of icomand interface</param>
            /// <returns>can execute or not</returns>
            public bool CanExecute(object parameter)
            {
                if (this.canExecute == null)
                {
                    return true;
                }

                return this.canExecute();
            }

            /// <summary>
            /// implement of icommand interface execute method
            /// </summary>
            /// <param name="parameter">parameter by default of icomand interface</param>
            public void Execute(object parameter)
            {
                this.execute();
            }
        }

        /// <summary>
        /// delegate command for view model
        /// </summary>
        public class DelegateCommand<T> : ICommand
        {
            #region members
            /// <summary>
            /// can execute function
            /// </summary>
            private readonly Func<T, bool> canExecute;

            /// <summary>
            /// execute function
            /// </summary>
            private readonly Action<T> execute;

            #endregion

            /// <summary>
            /// Initializes a new instance of the DelegateCommand class.
            /// </summary>
            /// <param name="execute">indicate an execute function</param>
            public DelegateCommand(Action<T> execute)
                : this(execute, null)
            {
            }

            /// <summary>
            /// Initializes a new instance of the DelegateCommand class.
            /// </summary>
            /// <param name="execute">execute function </param>
            /// <param name="canExecute">can execute function</param>
            public DelegateCommand(Action<T> execute, Func<T, bool> canExecute)
            {
                this.execute = execute;
                this.canExecute = canExecute;
            }

            /// <summary>
            /// can executes event handler
            /// </summary>
            public event EventHandler CanExecuteChanged
            {
                add { CommandManager.RequerySuggested += value; }
                remove { CommandManager.RequerySuggested -= value; }
            }

            /// <summary>
            /// implement of icommand can execute method
            /// </summary>
            /// <param name="parameter">parameter by default of icomand interface</param>
            /// <returns>can execute or not</returns>
            public bool CanExecute(object parameter)
            {
                if (this.canExecute == null)
                {
                    return true;
                }

                return this.canExecute((T)parameter);
            }

            /// <summary>
            /// implement of icommand interface execute method
            /// </summary>
            /// <param name="parameter">parameter by default of icomand interface</param>
            public void Execute(object parameter)
            {
                this.execute((T)parameter);
            }
        }
    }

  • 相关阅读:
    redis实现与分析
    NULL, '',0 '0'的区别
    Linux strace命令
    strcpy和memcpy的区别
    图书推荐
    php与mysql通讯那点事
    linux命令汇总
    linux系统信息查询及相关概念
    LNMP zabbix安装
    lftp查看文件时间与登录服务查看文件时间相差8小时
  • 原文地址:https://www.cnblogs.com/zisezhixin/p/3971961.html
Copyright © 2011-2022 走看看