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

 

 

 

 

转载于:https://www.cnblogs.com/akingyao/archive/2013/05/24/3097171.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值