【问题标题】:WPF RoutedCommand/RoutedEvent who/what raises the event?WPF RoutedCommand/RoutedEvent 谁/什么引发了事件?
【发布时间】:2017-10-17 01:42:50
【问题描述】:

我试图了解 WPF 如何实现路由命令,这些命令在内部由路由事件实现。在UIElement类定义中,我们找到了这段代码:

EventManager.RegisterClassHandler(type, CommandDevice.CommandDeviceEvent, new 
    CommandDeviceEventHandler(UIElement.OnCommandDeviceThunk), false);

从那时起,UIElement.OnCommandDeviceThunk 处理程序将通过它在其主体中调用的函数的参数传递所有信息,这些函数本身执行相同的操作,直到它到达我们订阅的CommandBinding.Executed 事件,从而执行我们的响应代码.

我似乎找不到并且想了解的是,谁提出了CommandDevice.CommandDeviceEvent

例如,当我将 ButtonBase 子类对象的 Command 属性链接到 ApplicationCommands 命令时,它会以某种方式引发 CommandDeviceEvent 吗?它是如何做到的?

我知道GlobalEventManager 包含许多事件处理程序列表,但我还不知道是谁引发事件来触发这些处理程序?

【问题讨论】:

    标签: c# wpf routed-events routed-commands


    【解决方案1】:

    例如,当我将 ButtonBase 子类对象的 Command 属性链接到 ApplicationCommands 命令时,它是否会以某种方式引发 CommandDeviceEvent 以及它是如何做到的?

    ButtonBase 类在点击时调用内部CommandHelpers 类的ExecuteCommandSource 方法:https://referencesource.microsoft.com/#PresentationFramework/Framework/System/Windows/Controls/Primitives/ButtonBase.cs,c0949dd81fca07f3,references

    此方法然后执行实际命令,即ICommand 属性返回的对象:https://referencesource.microsoft.com/#PresentationFramework/Framework/MS/Internal/Commands/CommandHelpers.cs,518e4547841e704d

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-26
      • 2013-07-25
      • 2020-09-24
      • 2011-05-15
      相关资源
      最近更新 更多