RoutedCommand 有什么用? 类构造函数所有者类型参数?

发布于 2024-07-22 04:51:17 字数 330 浏览 7 评论 0原文

RoutedCommand 的构造函数将“所有者类型”作为最后一个参数。 其意义何在? 什么时候使用?

MSDN 文档完全没有提供关于为什么需要它以及我是否可以对所有命令使用一种类型的任何线索

引用自 MSDN

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

还有一件事。 从名称数组动态创建新的路由命令时应该使用什么类型。 看起来任何类型都可以,所以我使用 UIElement,但如果有更适合的类型,我想知道。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧伤还要旧人安 2024-07-29 04:51:17

RoutedCommand 的源代码显示该类型成为 OwnerType 属性。 当获取InputGestures时,最终通过以下私有方法查询该属性。 因此,看起来好像该类型用于根据创建 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();
}

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();
}
稚气少女 2024-07-29 04:51:17

我知道这是一个非常古老的问题,但它是“routedcommand Ownertype”的热门搜索结果。

在每个 RoutedCommand 对象中存储 OwnerType 和 Name 会提示您如何在代码中查找对其的引用。 假设您正在某种具有任意 的方法上运行调试器ICommandSource 参数。 您可以检查 Command 属性,如果您看到 OwnerType 为 CommonCommands 并且 Name 为 "DoSomething",则可以导航到 CommonCommands 类的 DoSomething 字段,其中有可能是有用的注释,或者搜索对 CommonCommands.DoSomething 的引用以查找关联的 CommandBindings 或其他内容。 如果没有这些属性,RoatedCommand 将只是一个匿名对象。

我不知道这个原因是否是 API 设计者在添加参数时真正想到的,但它至少对我有用。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文