Lei*_*h S 2 delphi taction delphi-2010 tactionmanager
我正在尝试动态添加actionitems,我可以添加项目,当我这样做时它可以工作:
HostActionItem := ActionManager.ActionBars[0].Items[0].Items[2];
NewItem := HostAction.Items.Add;
NewItem.Action := MyActionToPerform;
NewItem.Caption := Description;
NewItem.ImageIndex := 1;
NewItem.Tag := 13;
Run Code Online (Sandbox Code Playgroud)
但是,当操作Execute方法触发时,我尝试从Sender对象获取ActionComponent,如下所示:
if (Sender is TAction) then
tag := (Sender As TAction).ActionComponent.Tag;
Run Code Online (Sandbox Code Playgroud)
但是ActionComponent总是为零.为什么ActionComponent没有初始化?
简答:
你期待着TActionClientItem出现ActionComponent一个TAction.这不会发生,因为TActionClientItem不会降临TComponent.
更长的答案:
我相信您正在将项目添加到菜单栏中.似乎是设计上TAction链接到菜单项不支持ActionComponent.菜单栏的项目是类型TActionClientItem.这是一个"集合项",而不是"组件".因此,ActionComponent当调用Execute所选项目的动作链接的方法时,菜单不能用菜单项填充参数.如果这听起来令人困惑,我想VCL来源的以下引用会说清楚:
TBasicActionLink.Execute 方法:
function Execute(AComponent: TComponent = nil): Boolean; virtual;
Run Code Online (Sandbox Code Playgroud)
传递的组件FAction.ActionComponent在执行之前被分配.
如何调用它TCustomActionMenuBar.ExecAction:
FSelectedItem.ActionLink.Execute;
Run Code Online (Sandbox Code Playgroud)
对于标题中的问题,我不认为你做错了什么,除了设置a Caption和ImageIndexa TActionClientItem是不必要的,因为它是TAction将显示的标题和图像.