繁体   English   中英

Delphi-TForm和泛型

[英]Delphi - TForm and Generics

我想知道在Delphi中是否有可能能够使用泛型定义从TForm派生的基本表单类。 我正在处理的应用程序与各种硬件设备(通过串行端口,USB,以太网等)交互,并且我希望每个设备都能够显示包含该设备特定属性的属性形式。

到目前为止,我有以下代码...

// DEVICE MODEL...

// Interface defining a device
IDevice = interface 
    procedure ShowPropertyForm;
    // ... Other procedures and functions
end;

// Abstract base device class
TDevice = class(IDevice)
    protected
        // Override this function to show their property form
        procedure DoShowPropertyForm; virtual; abstract;

    public
        // Calls Self.DoShowPropertyForm;
        procedure ShowPropertyForm;
end;

TSerialDevice = class(TDevice)
    protected
        // Creates and shows the TSerialDeviceForm below
        procedure DoShowPropertyForm; override;
end;

// Represents a device capable of providing positioning information
TGpsDevice = class(TSerialDevice)
    protected
        // Creates and shows the TGpsDeviceForm below
        procedure DoShowPropertyForm; override;
end;

// FORM MODEL...

// Represents a base form, with skinning functionality, etc
TBaseForm = class(TForm)

end;

// Base device properties form, allows the form to access a strongly-typed
// version of the IDevice
TDeviceForm<T : IDevice> = class(TBaseForm)

    private
        FDevice : T;

    public
        // Accessor for the associated IDevice
        property Device : T read FDevice write FDevice;
end;

// Property form for a TSerialDevice, has controls for controlling port name
// baud rate, stop/start bits, etc
TSerialDeviceForm = class(TDeviceForm<TSerialDevice>)

end;

// Property form for a TGpsDevice, has controls in addition to what is
// provided by the TSerialDeviceForm
TGpsDeviceForm = class(TSerialDeviceForm) 

end;

尝试访问表单设计器时会出现问题。 例如,TBaseForm包含一个“确定”和一个“取消”按钮。 我想向TDeviceForm添加其他功能,但是当我尝试打开设计器时,出现以下错误...

创建表单时出错:找不到根类:“”。

同样,如果尝试打开TGpsDeviceForm设计器,则会收到以下错误...

创建表单时出错:找不到“ TSerialDeviceForm”的祖先。

我假设Delphi表单设计器无法处理泛型,但是是否可以解决此问题呢?

在DFM文件中,对于除TBaseForm之外的所有内容,我已将第一行更改为:

对象DeviceForm:TDeviceForm继承到DeviceForm:TDeviceForm

但是,这似乎没有什么区别。

请谁能提供任何建议? 提前致谢!

目前,Delphi不支持DFM文件中存在泛型。 但是,考虑到您在一些评论中解释的内容后,我了解到您遇到的问题与我过去遇到的问题类似。

就我而言,我所做的是视觉形式继承和框架使用的结合。 更具体地讲,我必须创建表单的层次结构和框架的层次结构,并将它们一起使用,以便具有可以与特定对象一起使用的特定表单。 当处理给定的特定对象时,表单并不多,框架负责这种特定的处理。

这些框架具有正确的对象类型的属性,因此我从编译器获得了一定级别的类型检查,尤其是在最复杂的代码中,但是在某些部分中,我不得不自己检查类型,正是因为我无法使用泛型。

也许此模型将满足您的需求。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM