Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Delphi 如何实例化不同的帧类型?_Delphi_Frames_Metaclass_Class Reference - Fatal编程技术网

Delphi 如何实例化不同的帧类型?

Delphi 如何实例化不同的帧类型?,delphi,frames,metaclass,class-reference,Delphi,Frames,Metaclass,Class Reference,我带着相框再次来到这里。 我有一个主要的表格: 这只是为了理解框架的使用而创建的一个简单表单。 通过表单顶部的两个按钮,我想打开这两个框架: 框架1 和框架2 下面是第一帧的简单代码: unit AppFrame1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Fo

我带着相框再次来到这里。 我有一个主要的表格:

这只是为了理解框架的使用而创建的一个简单表单。 通过表单顶部的两个按钮,我想打开这两个框架:

框架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.
在这一点上,我的意图是使用界面来创建两个框架,但我不知道如何实现这一任务。 我的主要表格代码如下:

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? 我考虑过泛型,但我不知道如何实现这种接口,以便在用户选择打开时打开一个“泛型”框架,该框架可以从主框架中创建

我希望我已经清楚地解释了我的问题,但我知道这似乎很难理解

您的
TFrameClass
声明错误。现在,它被声明为TFrame的后代,TFrame只是另一个类

您需要的是:

由于
TFrame1
TFrame2
都是从
TFrame
派生而来,因此都可以放入
TFrameClass
变量中

但是我很确定这个
TFrameClass
已经存在于VCL中的某个地方,在这种情况下,您不必重新声明这个类型

随后,
FGenericFrame
私有字段的类型需要变成
TFrame
,而不是
TFrameClass


(另外,这与泛型无关。)

Hi-NGLN,我已经尝试按照您的说法设置TFrameClass,但在本例中,我收到了以下错误:E2010不兼容类型:“TFrameClass”和“TFrame”@Eros NGLN正在解决您提出的问题。如果您想像其他代码一样,那么您需要这样做。您不能期望我们调试您没有向我们展示的代码。请记住,我们不是调试服务。你仍然需要准备好做一些努力。嗨,大卫,你是对的,但是我尝试了很多东西,所以我没有发布最后的代码。我已经编辑了FramesManager代码,现在它是我在项目中的代码。Eros,关于你的第一条评论,请参阅我的编辑:
FGenericFrame
私有字段需要变成
TFrame
而不是
TFrameClass
。@Eros如果它有效,你应该帮NGLN一个忙并接受他的答案。
type
    TFrameClass = class(TFrame)
    end;
type
  TFrameClass = class of TFrame;