The constructor of the RoutedCommand has "owner type" as a last argument. What is its significance? When it is used?
RoutedCommand的构造函数的最后一个参数是“owner type”。它的意义是什么?当使用?
MSDN documentation gives completely no clue about why it's needed and whether I could use one type for all commands
MSDN文档完全没有说明为什么需要它,以及我是否可以为所有命令使用一个类型
Quote from MSDN
引用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.
还有一件事。从名称数组动态创建新的路由命令时应该使用什么类型。看起来任何类型都可以,我用的是UIElement,但是如果有更适合的类型,我想知道。
2 个解决方案
#1
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.
RoutedCommand的源显示该类型成为OwnerType属性。此属性在获取输入手势时,最终由以下私有方法查询。因此,看起来似乎这个类型被用于基于创建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();
}
#2
3
I know this is a very old question, but it's the top search hit for "routedcommand ownertype".
我知道这是一个很老的问题,但它是“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.
在每个RoutedCommand对象中存储一个owner类型和名称可以让您了解如何在代码中找到对它的引用。假设您正在某个具有任意ICommandSource参数的方法上运行调试器。您可以检查Command属性,如果您看到owner类型是CommonCommands, Name是“DoSomething”,您可以导航到CommonCommands类的DoSomething字段,那里可能有一个有用的注释,或者搜索对CommonCommands的引用。做点什么来找到相关的命令绑定之类的。如果没有这些属性,RoutedCommand将只是一个匿名对象。
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.
我不知道API设计者在引入参数时是否考虑到了这个原因,但至少对我来说是有用的。
#1
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.
RoutedCommand的源显示该类型成为OwnerType属性。此属性在获取输入手势时,最终由以下私有方法查询。因此,看起来似乎这个类型被用于基于创建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();
}
#2
3
I know this is a very old question, but it's the top search hit for "routedcommand ownertype".
我知道这是一个很老的问题,但它是“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.
在每个RoutedCommand对象中存储一个owner类型和名称可以让您了解如何在代码中找到对它的引用。假设您正在某个具有任意ICommandSource参数的方法上运行调试器。您可以检查Command属性,如果您看到owner类型是CommonCommands, Name是“DoSomething”,您可以导航到CommonCommands类的DoSomething字段,那里可能有一个有用的注释,或者搜索对CommonCommands的引用。做点什么来找到相关的命令绑定之类的。如果没有这些属性,RoutedCommand将只是一个匿名对象。
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.
我不知道API设计者在引入参数时是否考虑到了这个原因,但至少对我来说是有用的。