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

Edw*_*uay 3 c# mvvm

我正在研究Josh Smith的CommandSink示例,并且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, e.Parameter, out handled);
                        e.Handled = handled;
                    };
            }
        } 
        ...
Run Code Online (Sandbox Code Playgroud)

Eoi*_*ell 11

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

认为 (object Sender, EventArgs e)

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

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

相当于做

base.CanExecute += new EventHandler(myMethod_CanExecute);

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