我有一个简单的 IntraWeb 测试项目,我的 Unit1 有一个包含 3 个区域的 IWform:页眉、正文和页脚,如下所示:
type
TIWForm1 = class(TIWAppForm)
Body_Region: TIWRegion;
Header_Region: TIWRegion;
Footer_Region: TIWRegion;
public
end;
implementation
{$R *.dfm}
initialization
TIWForm1.SetAsMainForm;
end.
Run Code Online (Sandbox Code Playgroud)
我的Unit2和Unit3是一个IWFrame,它们只有一个按钮,如下所示:
type
TIWFrame2 = class(TFrame)
IWFrameRegion: TIWRegion;
Button1: TButton;
private
{ Private declarations }
public
{ Public declarations }
end;
implementation
{$R *.dfm}
end.
Run Code Online (Sandbox Code Playgroud)
(单元3与单元2相同)
现在,我可以在设计时将框架从工具板拖放到该区域,从而轻松地将框架分配给主体区域。
问题是如何在运行时将其更改为unit3 Frame?
如果我尝试将其添加到这样的类型部分
type
TIWForm1 = class(TIWAppForm)
Body_Region: TIWRegion;
Header_Region: TIWRegion;
Footer_Region: TIWRegion;
MyFram2: TIWFrame2; // added here
procedure IWAppFormShow(Sender: TObject);
public
end;
Run Code Online (Sandbox Code Playgroud)
系统尝试删除它!
如果我强迫保留它以用作
Body_Region.Parent := MyFram2;
Run Code Online (Sandbox Code Playgroud)
我的身体区域什么也没有!
如果我在设计时手动添加它,我会得到相同的声明,它可以工作,但我无法更改它!
我在这里遗漏了一些东西还是不可能这样做?
顺便说一句,我使用的是 Delphi Berlin 10.1 和 IW14.1.12。
“删除”声明的字段不是 IntraWeb 的事情,而是 Delphi 的“功能”。在“私有”部分中像这样声明它,否则它将被视为已发布:
TIWForm1 = class(TIWAppForm)
Body_Region: TIWRegion;
Header_Region: TIWRegion;
Footer_Region: TIWRegion;
procedure IWAppFormCreate(Sender: TObject); // use OnCreate event
private
FMyFram2: TIWFrame2; // put it inside a "Private" section.
FMyFram3: TIWFrame3;
public
end;
Run Code Online (Sandbox Code Playgroud)
删除 OnShow 事件并使用 OnCreate 事件代替。在 OnCreate 事件中创建框架实例,如下所示:
procedure TIWForm1.IWAppFormCreate(Sender: TObject);
begin
FMyFram2 := TIWFrame2.Create(Self); // create the frame
FMyFram2.Parent := Body_Region; // set parent
FMyFram2.IWFrameRegion.Visible := True; // set its internal region visibility.
// the same with Frame3, but lets keep it invisible for now
FMyFram3 := TIWFrame3.Create(Self);
FMyFram3.Parent := Body_Region;
FMyFram3.IWFrameRegion.Visible := False;
Self.RenderInvisibleControls := True; // tell the form to render invisible frames. They won't be visible in the browser until you make them visible
end;
Run Code Online (Sandbox Code Playgroud)
然后,您可以设置 Frame.IWFrameRegion 可见性,使一个可见而另一个不可见,如上所示。