Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What use has RoutedCommand' class constructor ownertype argument?

The constructor of the RoutedCommand has "owner type" as a last argument. What is its significance? When it is used?

MSDN documentation gives completely no clue about why it's needed and whether I could use one type for all commands

Quote from MSDN

ownerType
     Type: System.Type The type
     which is registering the command.

There is one more thing. What type should I use when creating new routed commands dynamically from array of names. It looks like that any type works, so I'm using UIElement, but if there is a more suited type for this I would like to know.

like image 945
Sergej Andrejev Avatar asked May 30 '09 18:05

Sergej Andrejev


2 Answers

The source for RoutedCommand shows that the type becomes the OwnerType property. This property is queried ultimately by the following private method when getting InputGestures. So it looks as though this type is being used to lookup a (hard-coded) set of Commands based on the type that created the RoutedCommand.

private InputGestureCollection GetInputGestures()
{
    if (this.OwnerType == typeof(ApplicationCommands))
{
    return ApplicationCommands.LoadDefaultGestureFromResource(this._commandId);
}
if (this.OwnerType == typeof(NavigationCommands))
{
    return NavigationCommands.LoadDefaultGestureFromResource(this._commandId);
}
if (this.OwnerType == typeof(MediaCommands))
{
    return MediaCommands.LoadDefaultGestureFromResource(this._commandId);
}
if (this.OwnerType == typeof(ComponentCommands))
{
    return ComponentCommands.LoadDefaultGestureFromResource(this._commandId);
}
return new InputGestureCollection();
}
like image 61
denis phillips Avatar answered Nov 24 '22 01:11

denis phillips


I know this is a very old question, but it's the top search hit for "routedcommand ownertype".

Storing an OwnerType and Name within each RoutedCommand object gives you a hint on how to find references to it in code. Suppose you are running the debugger on some method that has an arbitrary ICommandSource parameter. You can examine the Command property, and if you see that OwnerType is CommonCommands and Name is "DoSomething", you can navigate to the DoSomething field of the CommonCommands class, where there might be a useful comment, or search for references to CommonCommands.DoSomething to find associated CommandBindings or something. Without those properties, the RoutedCommand would just be an anonymous object.

I don't know if that reason was what the API designers actually had in mind when they included the argument, but it has been useful to me at least.

like image 32
nmclean Avatar answered Nov 24 '22 00:11

nmclean