我工作过Josh Smith's CommandSink Example和base.Executed += (s, e) =>...
结构都扔我,有人可以帮助使这个晶莹剔透?有人可以解释这个C#结构:base.Executed + =(S,E)=>
我的理解:
- base.CanExecute是在继承的类中的CommandBinding事件
- 的+ =是增加委托该事件
- 委托是匿名函数那条线
我不明白:
- 是(s,e)是该函数的签名?
- 其中是使用的变量?
这是在上下文中的代码:
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;
};
}
}
...
所以你也可以写(发送者,e)甚至是(whatevernnn,e),它只是一个占位符,对吗? – 2009-04-21 07:15:54
正确。 (s,e)或(jimbob,blahvar)签名就是您对参数的命名约定,以便您可以在匿名方法声明的其余部分引用它们。但是你应该坚持一些像(s,e)/(sender,e)/(sender,args)这样合理明显的东西 – 2009-04-21 07:20:26