Delphi 通用框架

Delphi generic frame

我还在这里带着关于 Delphi 帧的问题。 我想创建一个应用程序,它使用各种类型的框架来管理不同的数据库表,所以为了了解如何完成此类任务,我创建了一个简单的 Delphi 表单:

unit main;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
  Vcl.StdCtrls, Vcl.ExtCtrls, FramesManagement;

type
  TfrmMain = class(TForm)
    pnlCommands: TPanel;
    pnlFrames: TPanel;
    btnFrame1: TButton;
    btnFrame2: TButton;
    procedure FormCreate(Sender: TObject);
    procedure btnFrame1Click(Sender: TObject);
    procedure btnFrame2Click(Sender: TObject);
  private
    FFrame: IFrameManagement;
  public
    { Public declarations }
  end;

var
  frmMain: TfrmMain;

implementation

{$R *.dfm}

uses Frame1, Frame2;

procedure TfrmMain.FormCreate(Sender: TObject);
begin
  FFrame := TFramemanagement.Create;
end;

procedure TfrmMain.btnFrame1Click(Sender: TObject);
begin
  FFrame.CreateGenericFrame(pnlFrames, TFrame(Frame1.TFra1));
end;

procedure TfrmMain.btnFrame2Click(Sender: TObject);
begin
  FFrame.CreateGenericFrame(pnlFrames, TFrame(Frame2.TFra2));
end;

end.

此表单使用如下声明的接口:

unit FramesManagement;

interface

uses
  Vcl.Forms, Vcl.StdCtrls, Vcl.ExtCtrls, Frame1, Frame2;

type

  IFrameManagement = interface
  ['{A00E0D1B-3438-4DC4-9794-702E8302B567}']
    procedure CreateGenericFrame(ParentPanel: TPanel; FrameName: TFrame);
  end;

  TFrameManagement = class(TInterfacedObject, IFrameManagement)
  private
    genericFrame: TFrame;
    procedure CreateGenericFrame(ParentPanel: TPanel; FrameName: TFrame);
  end;

implementation

procedure TFrameManagement.CreateGenericFrame(ParentPanel: TPanel;
  FrameName: TFrame);
begin
  genericFrame := FrameName.Create(ParentPanel);
  genericFrame.Parent := ParentPanel;
end;

这里有两个框架。

第 1 帧:

unit Frame1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
  Vcl.StdCtrls;

type
  TFra1 = class(TFrame)
    txtFrame1: TStaticText;
    txtFrameType: TStaticText;
    lblFrameType: TLabel;
  private

  public

  end;

implementation

{$R *.dfm}

end.

和第 2 帧:

unit Frame2;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
  Vcl.StdCtrls;

type
  TFra2 = class(TFrame)
    txtFrame2: TStaticText;
    txtFrameType: TStaticText;
    lblFrameType: TLabel;
  private

  public

  end;

implementation

{$R *.dfm}

end.

这是所有代码,但是当我 运行 应用程序并尝试创建第一帧或第二帧时,我收到如下错误:

我认为解决方案可能是使用泛型,但我不知道如何使用它们。我的想法是对的还是有另一种方法可以达到这个目标? 谁能帮帮我?

procedure TFrameManagement.CreateGenericFrame(ParentPanel: TPanel; FrameName: TFrame);
begin
  genericFrame := FrameName.Create(ParentPanel);
  genericFrame.Parent := ParentPanel;
end;

这里 FrameName 是一个实例,您正在调用该实例的构造函数。您没有按照您的意愿创建新实例。

您需要使用元 类。

type
  TFrameClass = class of TFrame;

procedure TFrameManagement.CreateGenericFrame(ParentPanel: TPanel; FrameClass: TFrameClass);
begin
  genericFrame := FrameClass.Create(ParentPanel);
  genericFrame.Parent := ParentPanel;
end;

你可以这样称呼:

FFrame.CreateGenericFrame(pnlFrames, Frame2.TFra2);