我正在尝试动态添加操作项目,我可以添加该项目,并且当我这样做时它会起作用:
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
显示为 TAction
的 ActionComponent
。这种情况不会发生,因为 TActionClientItem
不是从 TComponent
派生的。
更长的答案:
我相信您正在将项目添加到菜单栏。链接到菜单项的 TAction
似乎是有意设计的,不支持 ActionComponent
。菜单栏的项目类型为 TActionClientItem
。这是一个“收藏品”,而不是一个“组件”。因此,在调用 Execute
时,菜单无法用菜单项填充 ActionComponent
参数。所选项目的操作链接的方法。如果这听起来令人困惑,我想下面来自 VCL 源代码的引用会清楚地说明这一点:
TBasicActionLink.Execute
方法:
function Execute(AComponent: TComponent = nil): Boolean; virtual;
传递的组件在执行之前被分配给FAction.ActionComponent
。
如何从TCustomActionMenuBar.ExecAction
调用它:
FSelectedItem.ActionLink.Execute;
对于标题中的问题,除了设置 TActionClientItem< 的
是不必要的,因为它将显示 Caption
和 ImageIndex
之外,我认为您没有做任何错误TAction
的标题和图像。
关于delphi - 如何以编程方式将操作添加到 Delphi 2010 中的操作管理器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4283491/