视觉继承还是用户控件?
我在 .net windows 应用程序中有多个表单,它们具有一组通用的文本框和其他用户输入控件,每个表单具有相同的外观和感觉,但每个表单都需要加载并保存到不同的数据库表。
将控件放置在基本表单上并从中继承,还是创建一个用户控件放在每个表单上会更好吗?
I have several forms in a .net windows app that have a common set of textboxes and other user input controls,each form has the same look and feel, but each form needs to load and save to different database tables.
Would it be better to place the controls on a base form, and inherit from it or to create a user control to drop on each form?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我通常更喜欢 UserControls,因为它们更灵活 - 您可以在表单上下文中或在任何其他视觉上下文中使用它们来显示或修改数据。 我遇到过几种情况,我必须将 UI 设计从单独的对话框更改为主窗体内的内联控件或类似的内容。
除此之外,我想说这是一个品味问题。
I usually prefer UserControls as they are more flexible - you can use them in the context of a form, or in any other visual context to display or modify data. I've had several cases where I had to change my UI design from a separate dialog to edit something to an inline control inside the main form or something like that.
Other than that, I'd say it's a matter of taste.
这取决于。 复合模型(使用用户控件)更清晰,但控件无法识别调用表单,因此您必须将逻辑作为事件传递。 这有时会导致重复的逻辑。
通过继承,您可以在基类中实现公共代码,并在子类形式中进行一些重写,并实现灵活的交互。 但这要求子类的所有实现者都了解基类源代码,这可能会变得混乱。
It depends. Composite model (using User Control) is cleaner, but the control cannot be aware of the calling form, so you have to pass the logic in as events. That sometimes can lead to duplicate logic.
With inheritance, you can implement common code at the base class with some override at the subclass form and implement flexible interplay. But that requires all implementers of the subclass to be aware of the base class source code, which can get chaotic.
我根据情况使用这两种方法。 如果您有一个逻辑功能块,您想将其不加修改地放入表单中,那么我认为 UserControls 是合适的。 但是,如果您只是希望跨多个表单复制和扩展通用 UI,那么视觉继承可能是更好的选择。
I use both methods, depending on the situation. If you have a functional piece of LOGIC that you want to just drop into a form unmodified, then I think that UserControls are appropriate. However, if you're just looking to duplicate and extend a common UI across multiple forms, then visual inheritance is probably the better option.
根据其更简单的使用与事件连接的用户控件背后的逻辑。 如果有共同的任务,您可以使用基类服务器控件。
Depending on the logic behind its simpler to use user controls wired up with events. If there are tasks in common you may use a base class Server Control.