[英]Generic forms and VS designer
I have a base class 我有一个基类
internal partial class View<T> : UserControl
where T : class
{
protected T t;
}
and I want to derive a child from View 我想从View派生一个孩子
internal partial class ViewChild<T> : View<T>
where T : class
{
}
it works OK, but I cannot edit ViewChild in the VS designer. 它工作正常,但我无法在VS设计器中编辑ViewChild。 I know the problem is generic base class.
我知道问题是通用基类。 However I do not understand how I can avoid that in this case.
但是我不明白在这种情况下我怎么能避免这种情况。 Is there any way to fix that?
有没有办法解决这个问题?
There is another way, and it doesn't rely on compiler flags: 还有另一种方法,它不依赖于编译器标志:
http://wonkitect.wordpress.com/2008/06/20/using-visual-studio-whidbey-to-design-abstract-forms/ http://wonkitect.wordpress.com/2008/06/20/using-visual-studio-whidbey-to-design-abstract-forms/
I really wouldn't advise the use of conditional compilation. 我真的不建议使用条件编译。 Much better to work with the framework, and not against it.
使用框架更好,而不是反对它。
Basically, you can give VS a different class through the existing framework. 基本上,您可以通过现有框架为VS提供不同的类。 You decorate your base class with a TypeDescriptionProvider attribute which tells VS to use a different class as a designer.
您使用TypeDescriptionProvider属性装饰您的基类,该属性告诉VS使用不同的类作为设计器。
As mentioned in the original blog post, there may be caveats associated with this workaround, but I got it working neatly on a project with > 25 UserControls inheriting from a common base class. 正如原始博客文章中所提到的,可能存在与此变通方法相关的警告,但我在一个项目上工作得很好,其中> 25个UserControl继承自公共基类。
Generics break the designer because it cannot instantiate the class without a type T
. 泛型破坏了设计者,因为它不能在没有类型
T
情况下实例化类。 I explain a workaround in my blog post: 我在我的博客文章中解释了一个解决方法:
http://adamhouldsworth.blogspot.co.uk/2010/02/winforms-visual-inheritance-limitations.html http://adamhouldsworth.blogspot.co.uk/2010/02/winforms-visual-inheritance-limitations.html
In short, you need to "resolve" the type with an intermediary class: 简而言之,您需要使用中间类“解析”类型:
BaseControl<T> : UserControl
CustomerControl_Design : BaseControl<Customer>
CustomerControl : CustomerControl_Design
You can then conditionally switch this class out of the code based on the DEBUG
or RELEASE
compiler switches: 然后,您可以根据
DEBUG
或RELEASE
编译器开关有条件地将此类从代码中切换出来:
#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
{
}
This will let you open the derived class of CustomerControl
, unfortunately you will never be able to design a UI control with generics in the signature. 这将允许您打开
CustomerControl
的派生类,遗憾的是,您永远无法在签名中设计带有泛型的UI控件。 My solution is only enabling the design of derived items. 我的解决方案只是启用派生项的设计。
I have no idea why CustomerControl : BaseControl<Customer>
won't work as in this case the type T
is defined, but it simply doesn't - I'm guessing because of the rules of generic usage. 我不知道为什么
CustomerControl : BaseControl<Customer>
将无法工作,因为在这种情况下定义了类型T
,但它根本没有 - 我猜测是因为通用的规则。
To their defense, Microsoft do say that this isn't supported. 为了他们的辩护,微软确实说这不受支持。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.