If you want to handle commands in CAB you have to use the AddInvoker method, for example on a menu item you’d have
[csharp]
this.Commands["SomeName"].AddInvoker(menuItem, “Click”);
[/csharp]
The listening method should be decorated with the CommandHandler attribute
[csharp]
[CommandHandler("SomeName")]
public void HandleIt(object sender, EventArgs e)
{
MessageBox.Show(“Works!”);
}
[/csharp]
Now here come the crucial remark and it took me half a day to discover it…
- the signature of the handling method has to be precisely (object sender, EventArgs e). If you use e.g. RoutedEventArgs it will not work. If fact nothing will do it except this couple of parameters.
- the method has to be public otherwise the ObjectBuilder will simply ignore it. Really, really annoying since usually you do not make handler of menu items public, do you?





hi,
In my project I found the this.Commands["SomeName"].AddInvoker(menuItem, “Click”); somewhat ackward.
The reason is that the control gets disabled when the commandstatus is disabled and hidden when unavailable.
If you just use the normal evenhandling delegates created by your view’s controls, this should work fine also.
The only reason why one should write such a signature is when you want to handle events genericaly. But then again you loose any rich eventhandler from implemented by your control.
I should’ve liked to see a method like :
this.Commands["SomeName"].AddInvoker(menuItem);
But he, that’s me!