我正在尝试动态添加操作项目,我可以添加该项目,并且当我这样做时它会起作用:
HostActionItem := ActionManager.ActionBars[0].Items[0].Items[2];
NewItem := HostAction.Items.Add;
NewItem.Action := MyActionToPerform;
NewItem.Caption := Description;
NewItem.ImageIndex := 1;
NewItem.Tag := 13;
但是,当操作 Execute 方法触发时,我尝试从 Sender 对象获取 ActionComponent,如下所示:
if (Sender is TAction) then
tag := (Sender As TAction).ActionComponent.Tag;
但 ActionComponent 始终为零。为什么 ActionComponent 没有被初始化?
简短回答:
你正在期待一个TActionClientItem
显示为ActionComponent
of an TAction
。从此以后就不会再发生这样的事了TActionClientItem
不下降自TComponent
.
更长的答案:
我相信您正在将项目添加到菜单栏。这似乎是设计使然TAction
链接到菜单项不支持ActionComponent
。菜单栏的项目是类型TActionClientItem http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/EN/html/delphivclwin32/ActnMan_TActionClientItem.html。这是一个“收藏品”,而不是一个“组件”。因此菜单不能填写ActionComponent
调用时带有菜单项的参数Execute http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/EN/html/delphivclwin32/Classes_TBasicActionLink_Execute.html所选项目的操作链接的方法。如果这听起来令人困惑,我想下面来自 VCL 源代码的引用会清楚地说明这一点:
TBasicActionLink.Execute
method:
function Execute(AComponent: TComponent = nil): Boolean; virtual;
传递的组件被分配给FAction.ActionComponent
在执行之前。
它是如何调用的TCustomActionMenuBar.ExecAction
:
FSelectedItem.ActionLink.Execute;
对于标题中的问题,我不认为你做错了什么,除了设置Caption
and ImageIndex
of a TActionClientItem
是不必要的,因为它是TAction
将显示的标题和图像。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)