CommandHandler and AddInvoker pairing, watch out for the signature!

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?

2 Responses to CommandHandler and AddInvoker pairing, watch out for the signature!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

top