【问题标题】:What is the ownerType of RoutedUICommand [duplicate]RoutedUICommand的ownerType是什么[重复]
【发布时间】:2018-01-30 13:08:28
【问题描述】:

我正在尝试了解RoutedUICommandconstructor 参数。

  • ownerType 参数的用途是什么?
  • nulltypeof(object) 传递为ownerType 是否有效?
  • 调用空构造函数时ownerType的值是多少?

我当前的上下文如下:

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)
        })
    );
}

请随时询问更多信息。我将非常感谢您的回答。谢谢!

【问题讨论】:

  • 注册命令的类型(在线文档中提到)是指声明你的SettingsCommand的类,即CustomApplicationCommands。
  • 如果您调用空构造函数,ownerType 将是 null。您可以在referencesource 找到源代码

标签: c# wpf routed-commands


【解决方案1】:

ownerType 参数的用途是什么?

它在内部用于convert commands to and from strings,例如,在读取或写入 Xaml 时。在computing the Text property中也用到了。

nulltypeof(object) 作为 ownerType 传递是否有效?
ownerType调用空构造函数时的值是多少?

是的,你可以传递null,这确实是调用默认构造函数时发生的情况。

【讨论】:

  • 比我快几秒!充实了您的答案,并在CommandConverter 中添加了指向更有趣用法的链接。
猜你喜欢
  • 1970-01-01
  • 2010-12-07
  • 2017-10-05
  • 2017-06-16
  • 2011-10-06
  • 2012-08-18
  • 1970-01-01
  • 2012-07-23
  • 2013-06-27
相关资源
最近更新 更多