不知对君胃口不,泛型之Form

ruanwei1987 2012-12-01 03:26:37
同创建和使用泛型类略有不同,通过一个中间类的继承,来达到可以在Vs2010上直接编辑泛型窗体.
步骤如下:
1、创建一个类BaseForm继承自Form,创建泛型T,然后可以加上限制.UI包含了界面上的button,不要用Design.cs,不然编译不过去.

2、创建一个中间件,这是一个类继承自BaseForm,它也不能有.design.cs文件.一定要带一个空的构造函数,不然后面的几面无法进入界面设计器.

3、创建一个Form,它继承自StringMiddleware

4、如果想创建一个Int32类型的界面怎么办呢?跳转到第二步,第三不.Int32Form 和StringForm的一些通用方法可以在BaseForm里面进行编写.
5、运行结果:



Demo下载:http://download.csdn.net/detail/ruanwei1987/4834043
...全文
187 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
ruanwei1987 2012-12-01
  • 打赏
  • 举报
回复
这个方法就是让设计器能直接编辑StringForm 或 Int32Form窗体 如果不加入StringMiddleware 那没办法在设计器上进行修改窗体. the problem is that the designer is always creating an instance of the base class - direct parent in the inheritance hierarchy. I'm assuming that's because you are changing the definition of the class in design time. So when you are designing MyEditorDialog, the designer actually has an instance of the System.Windows.Forms.Form class on which you add controls. But when you try to design MySubclassedDialog,the designer tries to create instance of the generic class MyEditorDialog<T> and fails. You get the same thing when you declare base form as abstract. The direct descendant can not be designed. The only solution (that I know of) is to add a third concrete, non-generic class in between, which will give your descendant form capability to be designed. Really simple, though you get an extra class that has no other reason to exist. 这是MSDN上的Vladimir.Ilic对关于泛型的解释
devmiao 2012-12-01
  • 打赏
  • 举报
回复
这样做法论坛里面有人做过,问题是设计器支持的不好,尤其是当你使用窗体继承的时候。 我想用它做一些特定的窗体会有些用途,但是不是一个通用的推荐的方式。
事理 2012-12-01
  • 打赏
  • 举报
回复
支持下分享,但有什么用途呢?
你的选择B 2012-12-01
  • 打赏
  • 举报
回复
LZ对泛型有深刻体会啊
kxloveh 2012-12-01
  • 打赏
  • 举报
回复
顶起
ruanwei1987 2012-12-01
  • 打赏
  • 举报
回复
我是用到了,就是泛型Usercontrol. 也说不来具体情形
hard_learner 2012-12-01
  • 打赏
  • 举报
回复
基于什么业务逻辑下会用到呢?

110,499

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧