RoutedCommand的类构造函数ownertype参数有什么用? [英] What use has RoutedCommand' class constructor ownertype argument?

查看:248
本文介绍了RoutedCommand的类构造函数ownertype参数有什么用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

RoutedCommand的构造函数的最后一个参数为所有者类型".它的意义是什么?什么时候使用?

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

MSDN文档完全没有说明为什么需要它以及我是否可以对所有命令使用一种类型

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

从MSDN引用

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

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

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.

推荐答案

RoutedCommand的源显示该类型成为OwnerType属性.获取InputGestures时,将通过以下私有方法最终查询此属性.因此,看起来这种类型正用于根据创建RoutedCommand的类型来查找(硬编码)命令集.

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

这篇关于RoutedCommand的类构造函数ownertype参数有什么用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆