将参数传递给delphi TFrame
为了避免单例和全局变量,我希望能够将参数传递给 TFrame 组件。 然而,由于 TFrame 通常在设计时包含在表单中,因此只能使用默认构造函数。
创建 TFrame 后,父窗体当然可以在 OnCreate 回调中设置一些属性。 然而,这并不能确保属性不会被遗忘,并且依赖关系不像使用构造函数那么清晰。
一个好的方法是,如果可以在读取 dfm 文件时注册一个用于创建组件的工厂。 然后,在工厂创建 TFrame 构造函数时,可以将所需的参数传递给 TFrame 构造函数。 有办法做到这一点吗?
或者有人对如何将参数传递给 TFrame 有更好的解决方案吗?
To avoid singletons and global variables I'd like to be able to pass parameters to a TFrame component. However since a TFrame normally is included on form at design time it is only possible to use the default constructor.
The parent form can of course set some properties in the OnCreate callback after the TFrame has been created. However this does not ensure that a property is not forgotten, and the dependencies are not as clear as using a constructor.
A nice way would be if it was possible to register a factory for creating components while the dfm file is being read. Then the required parameters could be passed to the TFrame constructor when created by the factory. Is there a way of accomplishing this?
Or does anyone have a better solution on how to pass parameters to a TFrame?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
所有组件(包括
TFrame
的后代)都需要能够使用从TComponent
继承的构造函数来构造。 否则,它们将无法在设计时正确使用。 如果您可以接受设计时使用的限制,那么您可以重写该构造函数并引发异常。 这将阻止在设计时将组件放置在表单上。 只需提供一些需要其他参数的其他构造函数即可。由于设计时要求,所有组件都需要能够在其部分或全部属性仍为其默认值的情况下存在。 这并不意味着组件在处于该状态时必须执行有用的操作,但它们确实需要能够无限期地保持在该状态。 例如,应该可以将组件放置在窗体上,保存窗体,然后关闭 Delphi,以便稍后恢复窗体设计。 即使尚未设置其所有属性以供最终使用,组件也应允许自身保存和恢复。
我的首选选择是仅在运行时强制执行组件的规则。 在允许使用所有属性之前,请检查所有属性是否都设置为合理的值。 您可以使用断言来强制正确使用组件。 如果您的课程的使用者尚未在其表单上完成您的组件设置,他们将学得很快。
All components, including descendants of
TFrame
, need to be able to be constructed using the constructor inherited fromTComponent
. Otherwise, they can't be used properly at design time. If the restriction of design-time use is acceptable to you, then you could override that constructor and raise an exception. That would prevent the component from being placed on a form at design time. Simply provide some other constructor that requires other parameters.Because of the design-time requirement, all components need to be able to exist with some or all of their properties still at their default values. That doesn't mean the components have to do useful things while they're in that state, but they do need to be able to stay in that state indefinitely. It should be OK, for example, to place a component on a form, save the form, and close Delphi, with the intention of resuming the form-designing at a later time. The component should allow itself to be saved and restored, even if all its properties haven't been set up for final use yet.
My preferred option is to enforce the component's rules only at run time. Check that all the properties are set to sensible values before you allow them to be used. You can use assertions to enforce the proper use of your components. Consumers of your classes will learn very quickly if they haven't finished setting up your components on their forms.
我通常会添加一个公共的、非虚拟的“初始化”或(对美国人初始化)过程,该过程需要提供所有参数。 然后这将设置属性。
如果可能的话,将属性设置为受保护或私有,因此设置它们的唯一方法是调用 Initialise(AFoo, ABar : integer)。
然后在TFormXXX.FormCreate或TformXXX.Create中,有:
I would normally add a public, non-virtual "Initialise" or (Initialize to Americans) procedure which requires all parameters to be provided. This will then set the properties.
Make the properties protected or private if possible, so the only way they can be set is from calling Initialise(AFoo, ABar : integer).
Then in TFormXXX.FormCreate or TformXXX.Create, have:
您可以创建/注册您自己的 tFrame 组件吗?
将其放在表单上 - 它的创建可以将任何内容传递给它。
could you create/registercomponent your own tFrame component and
place that on the form - it's create could have anything passed to it.
如果工厂可以提供您需要的参数,为什么不重写框架的默认构造函数,并向工厂类询问参数呢?
我通常制作自己的构造函数。 无论如何,我不喜欢在设计时创建框架。
If a factory could provide the parameters that you need, why don't you just override the default constructor for your frame, and ask the factory-class for parameters?
I usually make my own constructor. I don't like to create frames at designtime anyway.
a) 框架可以在需要时动态创建,在不需要时销毁
b) 为框架提供一个具有参数数据类型或数据结构的公共属性,并通过该属性将值传递给表单。
示例:
TAddress
- 保存地址常用元素的类。TAddressFra
- 带有可视控件的框架,用于显示地址TAddress
实例TAddressFra
实例带有
属性TAddress
实例的 TAddressFra.addresssetAddress(o_address : TAddress)
来分配TAddress
属性的值到TAddressFra
上相应的可视组件a) a frame can be created dynamically when required and destroyed when not needed
b) give the frame a public property with either the parameter data type or a data structure and pass the values to the form through the property.
Example:
TAddress
- a class to hold the usual elements of an address.TAddressFra
- a frame with the visual controls to display the addressTAddress
with valuesTAddressFra
TAddressFra.address
property with theTAddress
instancesetAddress(o_address : TAddress)
to assign the values of theTAddress
attributes to the corresponding visual components on theTAddressFra