2

我正在尝试动态添加操作项,我可以添加该项目,并且当我这样做时它可以工作:

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 没有被初始化?

4

1 回答 1

5

简短的回答:

您期望 aTActionClientItem出现ActionComponentTAction. 这不会发生,因为TActionClientItem它不是从TComponent.

更长的答案:

我相信您正在将您的项目添加到菜单栏。似乎是设计使TAction链接到菜单项的链接不支持ActionComponent. 菜单栏的项目是类型TActionClientItem。这是一个“收藏品”,而不是“组件”。ActionComponent因此菜单在调用Execute选中项的动作链接的方法时,不能用菜单项填写参数。如果这听起来令人困惑,我想 VCL 源代码中的以下引用会清楚地说明:

TBasicActionLink.Execute方法:

function Execute(AComponent: TComponent = nil): Boolean; virtual;

FAction.ActionComponent传递的组件在执行之前被分配。

如何调用它TCustomActionMenuBar.ExecAction

FSelectedItem.ActionLink.Execute;

For the question in the title, I don't think you're doing anything wrong, apart from setting the Caption and ImageIndex of a TActionClientItem is unnecessary, as it's the TAction's title and image which will be shown.

于 2010-11-26T17:29:31.043 回答