我带着框架又来了。 我有这个主要形式:
这只是为了理解框架的使用而创建的简单表单。 通过表单顶部的两个按钮,我想打开这两个框架:
帧1
和框架2
这里是第一帧的简单代码:
unit AppFrame1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
Vcl.StdCtrls;
type
TFrame1 = class(TFrame)
lblFrame1: TLabel;
private
{ Private declarations }
public
{ Public declarations }
end;
implementation
{$R *.dfm}
end.
这是第二帧的代码:
unit AppFrame2;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
Vcl.StdCtrls;
type
TFrame2 = class(TFrame)
lblFrame2: TLabel;
private
{ Private declarations }
public
{ Public declarations }
end;
implementation
{$R *.dfm}
end.
所以这两个框架没有什么特别的。 为了从主窗体打开框架,我创建了一个如下界面:
unit FramesManager;
interface
uses
Vcl.Forms, Vcl.StdCtrls, Vcl.ExtCtrls, Vcl.Controls;
type
TFrameClass = class(TFrame)
end;
IFrameManager = interface
['{A00E0D1B-3438-4DC4-9794-702E8302B567}']
procedure CreateGenericFrame(AParentPanel: TPanel; AFrameClass: TFrameClass);
procedure DestroyGenericFrame();
end;
TFrameManager = class(TInterfacedObject, IFrameManager)
private
FGenericFrame: TFrameClass;
procedure CreateGenericFrame(AParentPanel: TPanel; AFrameClass: TFrameClass);
procedure DestroyGenericFrame();
public
property Frame: TFrameClass read FGenericFrame write FGenericFrame;
end;
implementation
{ TFrameManagement }
procedure TFrameManager.CreateGenericFrame(AParentPanel: TPanel;
AFrameClass: TFrameClass);
begin
FGenericFrame := AFrameClass.Create(AParentPanel);
FGenericFrame.Parent := AParentPanel;
FGenericFrame.Align := alClient;
end;
procedure TFrameManager.DestroyGenericFrame;
begin
FGenericFrame.Free;
end;
end.
此时我的意图是使用接口(interface)来创建两个框架,但我不知道如何完成这个任务。 我的主要表单代码是这样的:
unit Main;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
Vcl.ExtCtrls, FramesManager, Vcl.StdCtrls, AppFrame1, AppFrame2;
type
TfrmMain = class(TForm)
pnlCommands: TPanel;
pnlFrames: TPanel;
btnCrtFrame1: TButton;
btnCrtFrame2: TButton;
procedure FormCreate(Sender: TObject);
procedure btnCrtFrame1Click(Sender: TObject);
procedure btnCrtFrame2Click(Sender: TObject);
private
FFrame: IFrameManager;
public
{ Public declarations }
end;
var
frmMain: TfrmMain;
implementation
{$R *.dfm}
procedure TfrmMain.FormCreate(Sender: TObject);
begin
FFrame := TFrameManager.Create;
end;
procedure TfrmMain.btnCrtFrame1Click(Sender: TObject);
begin
FFrame.CreateGenericFrame(pnlFrames, TFrame1);
end;
procedure TfrmMain.btnCrtFrame2Click(Sender: TObject);
begin
FFrame.CreateGenericFrame(pnlFrames, TFrame2);
end;
end.
当我尝试共同编译项目时,我收到此错误:
[dcc32 Error] Main.pas(41): E2010 Incompatible types: 'TFrameClass' and 'class of TFrame1'
[dcc32 Error] Main.pas(46): E2010 Incompatible types: 'TFrameClass' and 'class of TFrame2'
所以我想了解如何从主框架创建两个框架。 如何为 TFrameClass 分配正确的对象类型? 我已经考虑过泛型,但我不知道如何实现这种接口(interface),以便打开一个“通用”框架,当用户选择打开它时,可以从主框架中创建该框架。
我希望我已经清楚地解释了我的问题,但我知道这可能看起来很难理解。
最佳答案
type TFrameClass = class(TFrame) end;
您的TFrameClass
声明是错误的。现在它被声明为 TFrame 的后代,而 TFrame 只是另一个类。
您需要的是 class reference :
type
TFrameClass = class of TFrame;
由于 TFrame1
和 TFrame2
均源自 TFrame
,因此两者都可以放入 TFrameClass
变量中。
但我很确定这个 TFrameClass
已经存在于 VCL 中的某个位置,在这种情况下,您不必重新声明此类型。
随后,FGenericFrame
私有(private)字段的类型需要变为 TFrame
而不是 TFrameClass
。
(此外,这与泛型无关。)
关于delphi - 如何实例化不同的帧类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39743319/