0

I am trying to understand the constructor parameters of RoutedUICommand.

  • What is the ownerType argument for?
  • Is it a valid solution to just pass null or typeof(object) as ownerType?
  • What is the value of ownerType, when calling the empty constructor?

My current context is following:

public static class CustomApplicationCommands
{
    public static RoutedUICommand SettingsCommand = new RoutedUICommand(
        text: "Opens the settings window",
        name: nameof(SettingsCommand),
        ownerType: typeof(object), // ???
        inputGestures: new InputGestureCollection(new InputGesture[] {
            new KeyGesture(Key.F10)
        })
    );
}

Feel free to ask for more information. I will really appreciate your answers. Thank you!

ˈvɔlə
  • 9,204
  • 10
  • 63
  • 89
  • 2
    *The type that is registering the command* (mentioned in the online documentation) means the class that declares your SettingsCommand, i.e. CustomApplicationCommands. – Clemens Jan 30 '18 at 13:11
  • the `ownerType` will be `null` if you call the empty constructor. You can find source code over at [referencesource](http://referencesource.microsoft.com/#PresentationCore/Core/CSharp/System/Windows/Input/Command/RoutedUICommand.cs,0af85198d819d95b) – default Jan 30 '18 at 13:13

1 Answers1

2

What is the ownerType argument for?

It's used internally to convert commands to and from strings, e.g., when reading or writing Xaml. It's also used in computing the Text property.

Is it a valid solution to just pass null or typeof(object) as ownerType?
What is the value of ownerType, when calling the empty constructor?

Yes, you may pass null, and that is indeed what happens when calling the default constructor.

Mike Strobel
  • 25,075
  • 57
  • 69
mm8
  • 163,881
  • 10
  • 57
  • 88
  • 2
    Beat me by a few seconds! Fleshed out your answer a bit and added a link to the more interesting usage in `CommandConverter`. – Mike Strobel Jan 30 '18 at 13:24