如何以编程方式将操作添加到 Delphi 2010 中的操作管理器
我正在尝试动态添加操作项,我可以添加该项目,并且当我这样做时它会起作用:
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 没有被初始化?
I am trying to dynamically add actionitems, I can add the item and it works when I do this:
HostActionItem := ActionManager.ActionBars[0].Items[0].Items[2];
NewItem := HostAction.Items.Add;
NewItem.Action := MyActionToPerform;
NewItem.Caption := Description;
NewItem.ImageIndex := 1;
NewItem.Tag := 13;
However, when the action Execute method fires I attempt to get the ActionComponent from the Sender object like this:
if (Sender is TAction) then
tag := (Sender As TAction).ActionComponent.Tag;
But the ActionComponent is always nil. Why is the ActionComponent not being initialised?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简短回答:
您期望
TActionClientItem
显示为TAction
的ActionComponent
。这种情况不会发生,因为TActionClientItem
不是从TComponent
派生的。更长的答案:
我相信您正在将项目添加到菜单栏。链接到菜单项的
TAction
似乎是有意设计的,不支持ActionComponent
。菜单栏的项目的类型为TActionClientItem
。这是一个“收藏品”,而不是一个“组件”。因此,在调用 所选项目的操作链接的执行
方法。如果这听起来令人困惑,我想下面来自 VCL 源代码的引用会清楚地说明:TBasicActionLink.Execute
方法:传递的组件在它被分配之前被分配给
FAction.ActionComponent
被执行。如何从
TCustomActionMenuBar.ExecAction
调用:对于标题中的问题,除了设置
Caption
和之外,我认为您没有做任何错误的事情
是不必要的,因为将显示TActionClientItem
的 ImageIndexTAction
的标题和图像。short answer:
You're expecting a
TActionClientItem
to show up asActionComponent
of anTAction
. That won't happen sinceTActionClientItem
does not descend fromTComponent
.longer answer:
I believe you're adding your item to a menu bar. It seems to be by design that an
TAction
linked to a menu item would not support theActionComponent
. The items of a menu bar is of typeTActionClientItem
. This is a 'collection item', not a 'component'. Hence the menu cannot fill in theActionComponent
parameter with the menu item when calling theExecute
method of the action link of the selected item. If this sounds confusing, I guess the below quotes from the VCL source would make it clear:TBasicActionLink.Execute
method:The passed component is assigned to
FAction.ActionComponent
before it is executed.How it's called from
TCustomActionMenuBar.ExecAction
:For the question in the title, I don't think you're doing anything wrong, apart from setting the
Caption
andImageIndex
of aTActionClientItem
is unnecessary, as it's theTAction
's title and image which will be shown.