zoukankan      html  css  js  c++  java
  • ICommand in Silverlight

    let's see an instance that is easily and  can run well

      public class OpenChildWindowCommand : ICommand
        {
            /// <summary>
            /// 当出现影响是否应执行该命令的更改时发生。
            /// </summary>
            public event EventHandler CanExecuteChanged;
            /// <summary>
            /// 定义用于确定此命令是否可以在其当前状态下执行的方法。
            /// </summary>
            /// <param name="para"></param>
            /// <returns></returns>
            public bool CanExecute(object para)
            {
                if (para != null)
                {
                    CanExecuteChanged(para, new EventArgs());
                }
                return true;
                //return false;
            }
            /// <summary>
            /// 定义在调用此命令时调用的方法。
            /// </summary>
            /// <param name="para"></param>
            public void Execute(object para)
            {
                MessageBox.Show("afdafd");
    
            }
     
        }
    View Code

    and in xaml

    <UserControl.Resources>
            <dat:OpenChildWindowCommand x:Key="kk"></dat:OpenChildWindowCommand>
        </UserControl.Resources>
    <Button Grid.Row="2" Margin="0,0,4,0" Height="40" Width="40" 
                     Command="{StaticResource kk}" />

    and now you click this button you will see the messagebox.

    In order to understand the ICommand

    let's see it

      public interface ICommand
        {
            // Summary:
            //     Occurs when changes occur that affect whether the command should execute.
            event EventHandler CanExecuteChanged;
    
            // Summary:
            //     Defines the method that determines whether the command can execute in its
            //     current state.
            //
            // Parameters:
            //   parameter:
            //     Data used by the command. If the command does not require data to be passed,
            //     this object can be set to null.
            //
            // Returns:
            //     true if this command can be executed; otherwise, false.
            bool CanExecute(object parameter);
            //
            // Summary:
            //     Defines the method to be called when the command is invoked.
            //
            // Parameters:
            //   parameter:
            //     Data used by the command. If the command does not require data to be passed,
            //     this object can be set to null.
            void Execute(object parameter);
        }
    View Code

    Now it's clearly

    if we want to set the method to the model first we need construct the class like below

     public class CommandHandler:ICommand
        {
            Action<object> _act;
            bool _canExecute;
    
            public CommandHandler(Action<object> act, bool canExecute)
            {
                _act = act;
                _canExecute = canExecute;
            }
    
            public bool CanExecute(object parameter)
            {
                return _canExecute;
            }
    
            public event EventHandler CanExecuteChanged;
    
            public void Execute(object parameter)
            {
                _act(parameter);
            }
        }
    View Code

    and now in our model class

     private ICommand _SaveCommand;
            public ICommand SaveCommand
            {
                get
                {
                    return _SaveCommand = new CommandHandler(Save, _Execute);
                }
            }
            #endregion
    
            private void Save(object param)
            {
                ObservableCollection<Employee> newIM = new ObservableCollection<Employee>();
                foreach (Employee e in newIM)
                {
                    string a = e.FirstName;
                    string b = e.LastName;
                }
                 or othething
            }
    View Code
  • 相关阅读:
    【持续更新】leetcode算法-数组篇
    【转】敏捷开发之Scrum扫盲篇
    设计Twitter的api
    给一个表达式字符串加括号,计算它的所有的可能的值
    判断一个整数是否是平方数
    Spring Cloud 入门教程(七): 熔断机制 -- 断路器
    断路器(Curcuit Breaker)模式
    Spring Cloud 入门教程(六): 用声明式REST客户端Feign调用远端HTTP服务
    Spring Cloud 入门教程(五): Ribbon实现客户端的负载均衡
    Spring Cloud 入门教程(四): 分布式环境下自动发现配置服务
  • 原文地址:https://www.cnblogs.com/akingyao/p/3097171.html
Copyright © 2011-2022 走看看