What use has RoutedCommand' class constructor ownertype argument?
Asked Answered
F

2

11

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.

Flange answered 30/5, 2009 at 18:58 Comment(0)
C
6

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();
}
Cambodia answered 30/5, 2009 at 19:15 Comment(1)
Probably just "because." As good a reason for API design as any other.</sarcasm> blechInformality
R
4

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.

Reaganreagen answered 17/7, 2013 at 16:54 Comment(2)
So it has no effect at all? It's just for "documentation"?Nies
@ScottStafford It has a purpose. If you look at denis philips comment, the GetInputGestures private method loads the input gestures for the inbuilt commands. I would assume they still publicly expose this so that you assign a type that their internal code rejects.Featherweight

© 2022 - 2024 — McMap. All rights reserved.