【问题标题】:Delphi generic frameDelphi 通用框架
【发布时间】:2015-09-02 19:15:45
【问题描述】:

我仍然在这里询问有关 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;

这里有两个框架。

第一帧:

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.

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

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

【问题讨论】:

  • Generics在编程中有着非常特殊的含义,与C++模板密切相关。这不是非特定事物的正确标签:-)。
  • @Johan 没错,但是“通用”这个词也是一个非常通用的术语 :-) 可以使用这个词,只是不能用作标签。跨度>

标签: delphi frames


【解决方案1】:
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);

【讨论】:

    猜你喜欢
    • 2011-10-07
    • 1970-01-01
    • 2011-04-01
    • 2011-06-17
    • 2010-10-14
    • 2010-10-23
    • 2012-02-05
    • 2011-08-12
    • 1970-01-01
    相关资源
    最近更新 更多