1

我正在构建一个非常简单的 Firemonkey 项目 (RadStudio 10.3.3),以测试未来项目的某些布局选项。在过去的 VCL 中,我使用了模态形式。我正在测试的项目在主窗体 (Form1) 上使用面板(Panel1 和 Panel2)来嵌入两个附加窗体(Form2 和 Form3)。这两个嵌入的表单由每个表单上的单个列表框 (ListBox1) 组成。主窗体上的面板覆盖,所以我使用 Visibility 属性来显示我想要的嵌入窗体。所有代码都在主窗体上。

我遇到的问题是,当我在 Form2 和 Form3 之间切换时,加载到 Form3 上的列表框中的字符串永远不会出现。我在列表框和面板上尝试过 Repaint,在列表框上尝试过 InvalidateRect,在面板上尝试过 SetFocus 等,后面都是 Application.ProcessMessages。没有什么能成功。

主要代码是:

unit Unit1;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,
  FMX.Controls.Presentation, FMX.StdCtrls;

type
  TForm1 = class(TForm)
    Panel1: TPanel;
    Button1: TButton;
    Panel2: TPanel;
    Button2: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
    procedure EmbedForm(AParent:TControl; AForm:TCustomForm);
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.fmx}

uses Unit2, Unit3;

procedure TForm1.FormCreate(Sender: TObject);
begin
  // Embed Form2 in Panel1
  Application.CreateForm(TForm2, Form2);
  EmbedForm(Panel1, Form2);
  Panel1.Visible := true;

  // Embed Form3 in Panel2
  Application.CreateForm(TForm3, Form3);
  EmbedForm(Panel2, Form3);
  Panel2.Visible := false;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  // Populate ListBox1 on Form2 - the LOAD button
  Form2.ListBox1.Items.Add('Hello');
  Form2.ListBox1.Items.Add('World');
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  // Hide Panel1 (Form2) and show Panel2 (Form3)
  Panel1.Visible := false;
  Panel2.Visible := true;
  // Populate ListBox1 on Form3
  Form3.ListBox1.Items.Add('Goodbye');
  Form3.ListBox1.Items.Add('World');
  // Repaint (Here's why I have tried various things to get the listbox strings to show up)
  //Panel2.Repaint;
  //Form3.ListBox1.Repaint;
  //Application.ProcessMessages;
end;

procedure TForm1.EmbedForm(AParent: TControl; AForm: TCustomForm);
begin
  while AForm.ChildrenCount>0 do
    AForm.Children[0].Parent:=AParent;
end;

end.

表格2如下:

unit Unit2;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Layouts,
  FMX.ListBox;

type
  TForm2 = class(TForm)
    ListBox1: TListBox;
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

{$R *.fmx}

end.

Form3如下:

unit Unit3;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Layouts,
  FMX.ListBox, FMX.Controls.Presentation, FMX.StdCtrls;

type
  TForm3 = class(TForm)
    ListBox1: TListBox;
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form3: TForm3;

implementation

{$R *.fmx}

end.

根据要求,.fmx 文件如下所示。

Unit1.fmx (Form1):

object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 480
  ClientWidth = 640
  FormFactor.Width = 320
  FormFactor.Height = 480
  FormFactor.Devices = [Desktop]
  OnCreate = FormCreate
  DesignerMasterStyle = 0
  object Button1: TButton
    Position.X = 232.000000000000000000
    Position.Y = 448.000000000000000000
    TabOrder = 1
    Text = 'Load'
    OnClick = Button1Click
  end
  object Button2: TButton
    Position.X = 328.000000000000000000
    Position.Y = 448.000000000000000000
    TabOrder = 2
    Text = 'Next'
    OnClick = Button2Click
  end
  object Panel1: TPanel
    Align = Center
    Size.Width = 640.000000000000000000
    Size.Height = 393.000000000000000000
    Size.PlatformDefault = False
    TabOrder = 0
  end
  object Panel2: TPanel
    Position.Y = 43.000000000000000000
    Size.Width = 640.000000000000000000
    Size.Height = 393.000000000000000000
    Size.PlatformDefault = False
    TabOrder = 4
  end
end

Unit2.fmx (Form2):

object Form2: TForm2
  Left = 0
  Top = 0
  Caption = 'Form2'
  ClientHeight = 480
  ClientWidth = 640
  FormFactor.Width = 320
  FormFactor.Height = 480
  FormFactor.Devices = [Desktop]
  DesignerMasterStyle = 0
  object ListBox1: TListBox
    Align = Center
    TabOrder = 0
    DisableFocusEffect = True
    DefaultItemStyles.ItemStyle = ''
    DefaultItemStyles.GroupHeaderStyle = ''
    DefaultItemStyles.GroupFooterStyle = ''
    Viewport.Width = 200.000000000000000000
    Viewport.Height = 200.000000000000000000
  end
end

Unit3.fmx (Form3):

object Form3: TForm3
  Left = 0
  Top = 0
  Caption = 'Form3'
  ClientHeight = 480
  ClientWidth = 640
  FormFactor.Width = 320
  FormFactor.Height = 480
  FormFactor.Devices = [Desktop]
  DesignerMasterStyle = 0
  object ListBox1: TListBox
    Position.X = 8.000000000000000000
    Position.Y = 8.000000000000000000
    TabOrder = 1
    DisableFocusEffect = True
    DefaultItemStyles.ItemStyle = ''
    DefaultItemStyles.GroupHeaderStyle = ''
    DefaultItemStyles.GroupFooterStyle = ''
    Viewport.Width = 196.000000000000000000
    Viewport.Height = 196.000000000000000000
  end
end

同样,Form2 和 Form3 都只包含一个列表框(两者都包含 Listbox1)并且没有其他代码。我只需运行可执行文件,单击 Button1 显示 Hello World,然后单击 Button2 切换面板并显示第二个表单及其列表框。由于我是 Firemonkey 的新手,我确信我缺少一些简单的东西。感谢您的任何帮助!


解决方案非常简单。我不得不从项目的初始化设置中删除 Form2 和 Form3 的 CreateForm 事件——这对我来说是一个愚蠢的错误。它在执行过程中失去了对这些表格的引用。

4

2 回答 2

1

好的,感谢 Tom 的挫败感(再次,对不起 Tom!)和我自己的挫败感,我再次重建了测试应用程序。这一次,两个列表框都不会显示字符串。然后我开始考虑 Form2 和 Form3 失去与 Form1 的连接,因此我查看了 Tom 询问的 .fmx 文件。这导致我查看项目初始化文件,瞧!

program Project1;

uses
  System.StartUpCopy,
  FMX.Forms,
  Unit1 in 'Unit1.pas' {Form1},
  Unit2 in 'Unit2.pas' {Form2},
  Unit3 in 'Unit3.pas' {Form3};

{$R *.res}

begin
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  //Application.CreateForm(TForm2, Form2);  <-- NOW COMMENTED OUT
  //Application.CreateForm(TForm3, Form3);  <-- NOW COMMENTED OUT
  Application.Run;
end.

应用程序在启动时创建 Form2 和 Form2,然后我会在代码中创建重复项,从而导致引用丢失。我只是在启动时注释掉了 Form2 和 Form3 行,以防止应用程序创建它们并按预期工作。谢谢,汤姆!

于 2019-12-10T14:50:25.270 回答
1

我可以重现错误的唯一方法是将表单和两个面板之间的父子关系设置错误。

例如,我可以重现您描述的错误行为,如果Panel2是 的孩子Panel1,但如果它们都是表单的孩子,我无法重现问题。也许你不小心把第二个面板掉了Panel1?检查 IDE 中的结构窗口。


.fmx将文件添加到问题后进行编辑

查看提供Unit1.fmx的结尾如下:

  object Panel1: TPanel
    Align = Center
    Size.Width = 640.000000000000000000
    Size.Height = 393.000000000000000000
    Size.PlatformDefault = False
    TabOrder = 0
    object Layout1: TLayout
      Align = Contents
      Size.Width = 640.000000000000000000
      Size.Height = 393.000000000000000000
      Size.PlatformDefault = False
      TabOrder = 0
    end
  end

没有Panel2应有的根据Unit1.pas。相反,有一个Layout1as child to Panel1。因此,现在发布的文件与您昨天提供.fmx的文件不匹配。.pas但它可能仍然证实了我已经说过的话。

如果Panel2已被此布局替换,换句话说Panel2是 的孩子Panel1,那么这将准确解释您最初询问的行为。

于 2019-12-09T21:59:56.900 回答