我正在努力通过
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; };