Delphi-如何在按钮点击时调用ActionList?

时间:2017-02-24 05:21:46

标签: delphi delphi-xe

我在Delphi XE8中制作了一个多设备应用程序,它将LiveBindings用于数据集。

FMX有许多特定于LB的操作,包括TFMXBindNavigateDelete。我试图在按钮点击处理程序中使用它,如下所示:

按钮点击代码:

procedure TForm1.Button1Click(Sender: TObject);
begin
  if cdsOrdersSTATUS.Value='READY' then
  begin
    ShowMessage('Your Order Is Already READY/PENDING!');
  end
  else
  begin
    TAction(ActionList1.Actions[0]).Execute; //Not working,why?
  end;
end;

ActionList1的动作中的第一个(也是唯一的)项目是我的FMXBindNavigateDelete1。

问题是,即使代码TAction(ActionList1.Actions[0]).Execute执行,当前数据集记录已删除,因此显然 TFMXBindNavigateDelete的动作无效。为什么这样,我怎样才能使它工作?

产品图。 ActionList1:

ActionList

1 个答案:

答案 0 :(得分:4)

实际上,我认为这是一个很好的问题,并不值得投票。

我可以重现你的问题。我在FMX表格上放了两个按钮。我设置 按钮1点按到Button1Click和按钮2 ActionLiveBindingsBindNavigateDelete1

点击按钮2会弹出标准'删除记录?'确认并删除当前记录 如果我回答"是",正如所料。

但是,当点击Button1时,即使您的else块执行,也会删除记录?'确认 会出现,因此记录无法被删除。

原因在于代码

function TCustomAction.Execute: Boolean;
begin
  Result := False;
  if Supported and not Suspended then
  begin
    Update;
    if Enabled and AutoCheck then
      if (not Checked) or (Checked and (GroupIndex = 0)) then
        Checked := not Checked;
    if Enabled then
      Result := ((ActionList <> nil) and ActionList.ExecuteAction(Self)) or
        ((Application <> nil) and Application.ExecuteAction(Self)) or inherited Execute or
        ((Application <> nil) and Application.ActionExecuteTarget(Self));
  end;
end;

默认情况下,Enabled属性在调用期间设置为False Update所以if Enabled then ...永远不会执行。我找不到了 在调用Enabled期间将Update设置为True的方法。也许其他人知道如何做到这一点。

Button2的情况下,执行然后传递给TComponent.ExecuteAction和 它是对Action.ExecuteTarget(Self)的调用导致的 记录删除例程正在执行。

所以,从那以后,我的问题似乎就变成了如何调整代码 TComponent.ExecuteAction被执行,换句话说,如何关联 Action有一个组件。答案很明显。

所需要的就是这个

procedure TForm1.Button1Click(Sender: TObject);
begin
 if cdsOrdersSTATUS.Value='READY' then
  begin
    ShowMessage('Your Order Is Already READY/PENDING!');
  end
  else
  begin
    Button1.ExecuteAction(LiveBindingsBindNavigateDelete1);  //  <- this works
    //LiveBindingsBindNavigateDelete1.Execute; //Not working,why?
  end;
end;