泛型窗体继承时不能在设计器中显示,为神马?
2011-01-04 20:09
351 查看
今天遇到一奇怪的问题,自己搞了2个小时没有弄好。到Stack Overflow发了个帖子提问,顺便把问题也贴到博客里,如有人看到请帮忙。如果问题解决了,我会把问题的原因和解决方案放到这里来。
I meet a problem today. As following.
I create a generic Form ,
public class Form1<T>:Form
Then I create another inheritance form,
public class From2:Form1<string>.
The
form2 cannot be shown in the VS designer, the error message is "all
the classes in the file cannot be designed", (this error message is
translated from Chinese, the Chinese message is 文件中的类都不能进行设计).
But this program can be compiled successfully, and when it runs, both Form1 and From2 can work.
Anyone can give me some help about this ? Thanks.
I am not a native English speaker, I hope I have described my question clear.
----------------------------------------------
Stack Overflow果然是个好的编程网站,人气旺,水平高。5分钟后已经找到答案了。
以下为答案(原文
地址http://adamhouldsworth.blogspot.com/2010/02/winforms-visual-inheritance-limitations.html)
I have a small personal project going that involves a WinForms GUI. I
have a series of data listing and editing user controls that I decided
to refactor. There were 5 of each, and out of each they used roughly
80%-90% of the same code, just with naming changes.
A fantastic case of "same whitespace, different values"
. I read this a long time ago, but for some reason it stuck.
Supporting Generics
My first port of call was a base class for each of the two user
controls. However, I wanted to migrate even logic involving the
business class to the base class, which involved knowing about the
business class type.
Step in: Generics.
Step in: first problem
.
Using generics causes the designer interface in Visual Studio to
crash, as it breaks one of the rules about knowing the base class type.
Microsoft explicitly say generics are not supported. Fair enough.
The workaround for this (as specified in the previous link) is to
create a concrete implementation of the generic base class using a dummy
intermediary:
- BaseControl<T> : UserControl
- CustomerControl_Design : BaseControl<Customer>
- CustomerControl : CustomerControl_Design
The CustomerControl_Design class will not have any designer support,
and you shouldn't actually need it. But, your CustomerControl class now
does - the peasents rejoice.
I've wrapped the code in compiler tags, so that when it doesn't need design-time support, superfluous code is cut:
I meet a problem today. As following.
I create a generic Form ,
public class Form1<T>:Form
Then I create another inheritance form,
public class From2:Form1<string>.
The
form2 cannot be shown in the VS designer, the error message is "all
the classes in the file cannot be designed", (this error message is
translated from Chinese, the Chinese message is 文件中的类都不能进行设计).
But this program can be compiled successfully, and when it runs, both Form1 and From2 can work.
Anyone can give me some help about this ? Thanks.
I am not a native English speaker, I hope I have described my question clear.
----------------------------------------------
Stack Overflow果然是个好的编程网站,人气旺,水平高。5分钟后已经找到答案了。
以下为答案(原文
地址http://adamhouldsworth.blogspot.com/2010/02/winforms-visual-inheritance-limitations.html)
I have a small personal project going that involves a WinForms GUI. I
have a series of data listing and editing user controls that I decided
to refactor. There were 5 of each, and out of each they used roughly
80%-90% of the same code, just with naming changes.
A fantastic case of "same whitespace, different values"
. I read this a long time ago, but for some reason it stuck.
Supporting Generics
My first port of call was a base class for each of the two user
controls. However, I wanted to migrate even logic involving the
business class to the base class, which involved knowing about the
business class type.
Step in: Generics.
Step in: first problem
.
Using generics causes the designer interface in Visual Studio to
crash, as it breaks one of the rules about knowing the base class type.
Microsoft explicitly say generics are not supported. Fair enough.
The workaround for this (as specified in the previous link) is to
create a concrete implementation of the generic base class using a dummy
intermediary:
- BaseControl<T> : UserControl
- CustomerControl_Design : BaseControl<Customer>
- CustomerControl : CustomerControl_Design
The CustomerControl_Design class will not have any designer support,
and you shouldn't actually need it. But, your CustomerControl class now
does - the peasents rejoice.
I've wrapped the code in compiler tags, so that when it doesn't need design-time support, superfluous code is cut:
#if DEBUG namespace MyNamespace { using System; public partial class CustomerEditorControl_Design : BaseEditorControl<Customer> { public CustomerEditorControl_Design() : base () { InitializeComponent();
} } } #endif
public partial class CustomerEditorControl
#if DEBUG : CustomerEditorControl_Design
#else : BaseEditorControl<Customer>
#endif
{ ... }
相关文章推荐
- 泛型窗体继承时不能在设计器中显示,为神马?
- vs C# 继承窗体类出现“文件中的类都不能进行设计,因此未能为该文件显示设计器。设计器检查出文件中有以下类”无法打开设计器的错误
- 用C++设计一个不能被继承的类
- 开发自己的窗体设计器(PropertyGrid显示中文属性名)
- C#基础系列:开发自己的窗体设计器(PropertyGrid显示中文属性名)
- 自定义UserControl的属性为什么不能在设计时显示在属性窗口中
- 结合RibbonControl设计MDI窗体,在子窗体关闭后,顶部显示额外的控制栏残影
- 文件中的类都不能进行设计,因此未能为该文件显示设计器。设计器检查出文件中有以下类: FormMain --- 未能加载基类
- C# 文件里的类不能进行设计,因此未能为该文件显示设计器
- winform窗体继承泛型类时,设计器无法使用解决办法
- 用C++ 设计一个不能被继承的类。
- C# winform中不规则窗体制作的解决方案(已经解决24位色以上不能正常显示问题)
- C++设计一个不能被继承的类
- C# winform中不规则窗体制作的解决方案(已经解决24位色以上不能正常显示问题)
- C#基础系列:开发自己的窗体设计器(PropertyGrid显示中文属性名)
- Developer Express 之 XtraReport如何显示设计窗体
- C# winform中不规则窗体制作的解决方案(已经解决24位色以上不能正常显示问题)
- WinForm应用程序框架设计之WinAction(一:显示列表窗体)
- MDI和继承窗体设计
- 用C++设计一个不能被继承的类