RoutedCommand的类构造函数ownertype参数有什么用?

Ser*_*jev 9 wpf routed-commands

RoutedCommand的构造函数将"owner type"作为最后一个参数.它的意义是什么?什么时候使用?

MSDN文档完全不知道为什么需要它以及我是否可以为所有命令使用一种类型

从MSDN引用

ownerType
     Type: System.Type The type
     which is registering the command.
Run Code Online (Sandbox Code Playgroud)

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

den*_*ips 6

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();
}
Run Code Online (Sandbox Code Playgroud)

  • 可能只是"因为".API设计与其他任何设计一样好.</ sarcasm>*blech* (2认同)