【问题标题】:How to instantiate different frame types?如何实例化不同的帧类型?
【发布时间】:2017-02-06 04:28:55
【问题描述】:

我又带着相框来了。 我有这个主要形式:

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

第一帧

和Frame2

这里是第一帧的简单代码:

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? 我对泛型有所了解,但我不知道如何实现这种接口以打开一个“泛型”框架,当用户选择打开它时,可以从主框架中创建它。

我希望我已经清楚地解释了我的问题,但我知道理解起来可能看起来很复杂。

【问题讨论】:

    标签: delphi frames metaclass class-reference


    【解决方案1】:
    type
        TFrameClass = class(TFrame)
        end;
    

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

    你需要的是class reference:

    type
      TFrameClass = class of TFrame;
    

    因为TFrame1TFrame2 都源自TFrame,所以两者都可以放入TFrameClass 变量中。

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

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

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

    【讨论】:

    • 您好 NGLN,我已经尝试按照您的说法设置 TFrameClass,但在这种情况下,我收到此错误:E2010 不兼容的类型:'TFrameClass' 和 'TFrame'
    • @Eros NGLN 正在解决您提出的问题。如果您想改用其他代码,则需要这样做。您不能指望我们调试您没有向我们展示的代码。请记住,我们不是调试服务。您仍然需要准备好付出一些努力。
    • 嗨大卫,你是对的,但我已经尝试了很多东西,所以我没有发布最后一个代码。我已经编辑了 FramesManager 代码,现在它是我项目中的代码。
    • Eros,关于您的第一条评论,请参阅我的编辑:FGenericFrame 私有字段需要成为 TFrame 而不是 TFrameClass
    • @Eros 如果可行,您应该帮 NGLN 一个忙并接受他的回答。
    猜你喜欢
    • 1970-01-01
    • 2011-01-01
    • 2017-09-07
    • 1970-01-01
    • 1970-01-01
    • 2018-04-19
    • 1970-01-01
    • 2019-02-15
    相关资源
    最近更新 更多