加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

有人可以解释这个C#结构:base.Executed =(s,e)=>

发布时间:2020-12-15 19:30:22 所属栏目:百科 来源:网络整理
导读:我正在努力通过 Josh Smith’s CommandSink Example和base.Executed =(s,e)= …结构正在抛弃我,有人可以帮助使这个晶莹剔透吗? 我的理解: base.CanExecute是继承的类CommandBinding上的事件 =正在为该事件添加委托 委托是跟随该行的匿名函数 我不明白的是
我正在努力通过 Josh Smith’s CommandSink Example和base.Executed =(s,e)=> …结构正在抛弃我,有人可以帮助使这个晶莹剔透吗?

我的理解:

> base.CanExecute是继承的类CommandBinding上的事件
> =正在为该事件添加委托
>委托是跟随该行的匿名函数

我不明白的是:

>(s,e)是该函数的签名吗?
>变量s在哪里使用?

这是上下文中的代码:

public class CommandSinkBinding : CommandBinding
    {
        #region CommandSink [instance property]

        ICommandSink _commandSink;

        public ICommandSink CommandSink
        {
            get { return _commandSink; }
            set
            {
                if (value == null)
                    throw new ArgumentNullException("Cannot set CommandSink to null.");

                if (_commandSink != null)
                    throw new InvalidOperationException("Cannot set CommandSink more than once.");

                _commandSink = value;

                base.CanExecute += (s,e) =>
                    {
                        bool handled;
                        e.CanExecute = _commandSink.CanExecuteCommand(e.Command,e.Parameter,out handled);
                        e.Handled = handled;
                    };

                base.Executed += (s,e) =>
                    {
                        bool handled;
                        _commandSink.ExecuteCommand(e.Command,out handled);
                        e.Handled = handled;
                    };
            }
        } 
        ...

解决方法

(s,e)是事件处理程序的方法参数签名(在这种情况下是定义的anoymous方法)

想(对象发件人,EventArgs e)

s参数只是没有在方法的其余部分中使用,这很好.必须在那里匹配预期的签名

base.CanExecute += (s,out handled);
                        e.Handled = handled;
                    };

相当于做

base.CanExecute += new EventHandler(myMethod_CanExecute);

///....
protected void myMethod_CanExecute(object sender,EventArgs e)
{
    bool handled;
    e.CanExecute = _commandSink.CanExecuteCommand(e.Command,out handled);
    e.Handled = handled;
};

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读