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

  • 相关阅读:
    112. Path Sum
    66. Plus One
    258. Add Digits
    268. Missing Number
    275. H-Index II
    274. H-Index
    264. Ugly Number II
    263. Ugly Number
    199. Binary Tree Right Side View
    222. Count Complete Tree Nodes
  • 原文地址:https://www.cnblogs.com/zisezhixin/p/3971961.html
Copyright © 2011-2022 走看看