通用 Windows 窗体模板

发布于 2024-12-25 19:49:09 字数 684 浏览 2 评论 0原文

我正在创建一个通用 Windows 窗体,它接受 T 并使用带有自定义属性的反射在运行时创建标签和输入控件。

示例:

class GenericForm<T>: Form where T : ICloneable<T>  
{  
}  

以下是表单代码的上一个问题的链接: 所以问题

该表单可以接受以下实体类作为示例:

class Vehicle: ICloneable<Vehicle>  
{  
    public int Id { get; set; }  
    public int Name { get; set; }  
    public int Description { get; set; }  
}  

正如您可以想象的那样,表单背后的魔力将使用反射来确定数据类型、验证标准、要使用的首选控件类型等。

我不是重新发明轮子,而是认为如果有人知道这样的框架,那么值得询问一下。不用说,我正在寻找简单的东西而不是庞大的框架。

I am creating a generic Windows Form that accepts T and uses reflection with custom attributes to create labels and input controls at run-time.

Example:

class GenericForm<T>: Form where T : ICloneable<T>  
{  
}  

Here's a link to a previous question for the form code: SO Question.

This form could accept the following entity class as an example:

class Vehicle: ICloneable<Vehicle>  
{  
    public int Id { get; set; }  
    public int Name { get; set; }  
    public int Description { get; set; }  
}  

As you could imagine, the magic behind the form would use reflection to determine data types, validation criteria, preferred control types to use, etc.

Rather than re-inventing the wheel, I thought it would be worth asking on SO if anyone knows of such frameworks. Needless to say, I'm looking for something simple rather than a bulky framework.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

诗化ㄋ丶相逢 2025-01-01 19:49:09

eXpressApp Framework (XAF) 可以动态生成 UI。在简单的情况下,程序员只会创建业务实体,根本不关心 UI。

eXpressApp Framework (XAF) can generate UI on the fly. In a simple case, a programmer will create business entities only, and will not care of UI at all.

痴情 2025-01-01 19:49:09

据我所知,没有任何框架可以在运行时生成 UI 代码。之前有很多工具(代码生成器)可以执行此操作。但是您不会拥有“仅”更改代码的优势 - 您需要启动代码生成器的额外步骤。

如果您确实想在运行时创建 UI 信息 - 我会为您的属性生成属性,这将告诉您的 UI 生成器如何处理此属性(如果没有给出属性 - 为您的数据类型设置默认值)。这需要大量编码,但可以为您将来的中小型项目节省时间。

您可以做的另一件事是将 UI 信息外部化到 XML 文件中,并为该文件提供一个生成器。实际上有一个框架可以做到这一点 - 查看 re-motion 框架。我不知道 UI 部分是否免费,但它有一些功能(即 mixins)可以帮助您完成任务。

As far as I know, there are no frameworks that generate the UI code at runtime. There are plenty of tools (code-generators) that do this before. But you wouldn't have the advantage of "only" changing the code - you'd had an extra step where you would need to start the code generator.

If you really want to create the UI information at runtime - I'd generate Attributes for your properties, that would tell your UI generator how to deal with this property (if no Attribute is given - have a default for your datatypes). It's a lot of coding but could save you time for small to medium projects in the future.

Another thing you could do is to externalize your UI information into an XML file and have a generator for that one. There's actually a framework that does that - have a look at the re-motion framework. I don't know if the part of the UI is free but it has some functionality (i.e. mixins) that could help you fulfilling your task.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文