C# 中的动态 UI 生成
我正在为图书馆设计一个应用程序。不是一个大型图书馆,而是一个规模非常小的图书馆,我的主要任务是保存有关书籍的信息。但这个图书馆应用程序应该能够适应任何专业人士的私人图书馆。例如,对于律师来说,除了书籍的基本信息(标题、作者、出版商等)之外,可能还存在与书籍相关的其他特殊字段(案件编号、法院编号等)。医生的书可能还具有其他一些特殊属性。其他职业也是如此。
因此,我将使用 SQL Server CE 数据库,并且希望有一个具有常用属性的 BOOK 表,并根据需要更改该表以满足特殊需求(添加更多列)。
但我担心的是动态生成 GUI 以支持新属性。
有什么方法可以解决动态 GUI 生成问题吗?
我并不要求完整的代码(显然我不会得到),但如果您确实有任何编码来支持该方法,请友善地发布它:)
有什么我应该知道的关于优点、缺点、死胡同的信息吗? 、注意事项或警告等?
I am designing an application for a library. Not a large scale library, but a very small scale library where my primary task is to just keep information about the books. But this library application should be able to adapt to any professional's private library. For an example, for a lawyer, apart from the basic information about the book (title, author, publisher etc), there maybe other special fields that is associated with a book (Case Number, Court Number etc). A doctor may have some other special attributes for a book. Same goes for other professions.
So I am going to use an SQL Server CE database and I am hoping to have a BOOK table with the usual attributes and on demand ALTER the table to suit the special needs (add more columns).
But my worry is generating the GUI dynamically to support the new attributes.
Are there any approaches to tackle the dynamic GUI generation?
I am not asking for complete code (which obviously I wont get), but if you do have any coding to support the approach, please be kind enough to post it :)
Is there anything I should know about the pros, cons, dead ends, cautions or warnings etc?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 @devnull 选取的数据模型方面,您正在描述自定义字段实现,而 @devnull 正在描述 EAV 模型。
stackoverflow 有一篇很好的文章,介绍了应用程序中自定义字段的设计模式:
支持应用程序中自定义字段的设计模式是什么?
数据模型选择和 UI 生成紧密相关,因此在决定您的 UI 生成之前,您无法真正回答 UI 生成问题数据模型/自定义字段模式。我最初的反应与 @devnull 对改变方法的反应相同,但确实没有很好的解决方案。
如果您拥有所有可能字段的超集,并允许用户启用/禁用适合其应用程序域的字段,则可以降低很多复杂性。我已经与非常聪明的人一起在应用程序中完成了自定义字段的多次实现,但这总是很困难。如果您足够了解应用程序领域,您就可以远离超级灵活的架构,并为自己省去很多麻烦。
请注意,一个重要的考虑因素是他们是否需要查询自定义字段。如果不需要支持一般查询,那就容易多了。您只需插入 userdate1、usern 等并为标签提供元数据表。
On the data model side that @devnull picked up, you are describing a custom fields implementation and @devnull is describing the EAV model.
There's a good stackoverflow article that covers design patterns for custom fields in an application:
What are design patterns to support custom fields in an application?
The data model choice and the UI generation are tightly linked, so you can't really answer the UI generation question until you decide on your data model/custom field pattern. My initial reaction was the same as @devnull's on the alter approach, but there really is no great solution.
You can reduce a lot of complexity if you have a superset of all possible fields and allow the user to enable/disable the ones that are appropriate to their application domain. I've done several implementations of custom fields in an application with very smart people and it's always hard. If you understand the application domain well enough you can stay away from the uber-flexible architectures and save yourself a lot of grief.
Note that an important consideration is whether they will need to query on the custom fields. It's much easier if you don't have to support general querying. You just slot userdate1, usern, etc. and provide a metadata table for labels.
我不知道动态更改表格是否是一个好的设计决策。您可以使用一个查找表(可以在其中定义详细信息类型)和一个图书详细信息表(可以在其中存储这些详细信息)。然后,您可以以数据网格的形式在图书编辑部分中显示这些详细信息,该数据网格的详细信息类型为行,每行都有一个您可以在其中编辑值的列。当然,一本书的详细信息可以是除简单字符串值以外的任何内容,但这可以轻松处理。希望我说得足够清楚:)
I don't know if altering the table dynamically is a good design decision. You could instead have a lookup table in which you can define detail types, and a books detail table in which you would store these details. Then you can display these details in the book editing section in the form of a datagrid that has the detail types as rows, each row having a column in which you would edit the value. Of course, the detail of a book can be anything else than a simple string value but this can be handled with ease. Hope i was clear enough :)
有很多可用的代码生成工具。其中一些生成带有易于使用的 GUI 的代码。
MyGeneration
CodeNGen
CodeSmith
IgnyteDataLayerGen
NetGenerationCodeGen
OxyGen 代码生成器
.NetTiers
CodeThatBuilder
CslaGenerator
CodeBreeze
或者,以下代码可以让您的生活更轻松。
您可以为实体拥有一个通用基本表单,如下所示:
以及集合的通用基本表单:
然后您的所有表单都将具有相同的通用外观:
There are lots of code generation tools available. Some of them generates code with readily usable GUI.
MyGeneration
CodeNGen
CodeSmith
IgnyteDataLayerGen
NetGenerationCodeGen
OxyGen Code Generator
.NetTiers
CodeThatBuilder
CslaGenerator
CodeBreeze
Alternatively the following codes can make your life more easier.
You can have a general base form for entities like this:
And a general base form for Collections:
Then all of your forms will have the same general looks:
这可能会有所帮助,它不是动态的,但可以很容易地进行调整。
http://zxtrix.wordpress.com/2010/11/13/automatic-winforms-ui- Generation-from-object-metadata/
This might help, its not dynamic but it could be adapted easily enough..
http://zxtrix.wordpress.com/2010/11/13/automatic-winforms-ui-generation-from-object-metadata/