代码之家  ›  专栏  ›  技术社区  ›  SignalOne

德尔福(FMX)循环参考

  •  -1
  • SignalOne  · 技术社区  · 1 年前

    我对Delphi FMX中的“无法在组件之间创建循环依赖”感到疯狂。我正在以编程方式在窗体上创建控件,其中一部分是标签,在网格内、滚动框内、网格内。

    准则的相关部分包括:

    Layout := TGridPanelLayout.Create(self);
    Layout.Parent := self;
    Panel := TVertScrollBox.Create(self);
    Panel.Parent := Layout;
    Form := TGridPanelLayout.Create(self);
    Form.Parent := Panel;
    FormLabel := TLabel(self);
    FormLabel.Parent := Form;
    

    它在我得到错误的最后一行。

    具体来说,FMX中的第4387行。类型引发错误:

    procedure TFmxObject.SetParent(const Value: TFmxObject);
    begin
      if Value = Self then
        Exit;
      if FParent <> Value then
      begin
        if IsChild(Value) then
           raise EInvalidOperation.Create(SCannotCreateCircularDependence);
        if FParent <> nil then
          FParent.RemoveObject(Self);
        if Value <> nil then
          Value.AddObject(Self)
        else
          FParent := Value;
      end;
    end;
    

    有人知道为什么会在一系列新的控制措施下提出这个问题吗?

    有些代码没有包含在内,但它设置了行/列,不相关,但如果需要,我可以发布。

    谢谢。

    1 回复  |  直到 1 年前
        1
  •  2
  •   Remy Lebeau    1 年前

    FormLabel := TLabel(self);

    这条线错了。你是 类型铸造 Self 本身,而不是 创建 一个新的标签 自我 拥有。因此,您确实创建了一个循环引用:

    • Layout 是一个孩子 自我 好的

    • Panel 是一个孩子 布局 好的

    • Form 是一个孩子 面板 好的

    • FormLabel 又称作 自我 )是一个孩子 表格 =错误! 自我 它不可能是自己的(孙子)。

    行应该是这样的:

    FormLabel := TLabel.Create(self);