C# WinForm UserControl 的通用基类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/677609/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Generic base class for WinForm UserControl
提问by Filini
I created a generic base class for a WinForm UserControl:
我为 WinForm UserControl 创建了一个通用基类:
public partial class BaseUserControl<T> : UserControl
{
public virtual void MyMethod<T>()
{
// some base stuff here
}
}
And a UserControl based on that:
以及基于此的 UserControl:
public partial class MyControl : BaseUserControl<SomeClass>
{
public override void MyMethod<SomeClass>()
{
// some specific stuff here
base.MyMethod<SomeClass>();
}
}
It works fine, but MyControl cannot be edited in the VisualStudio Designer, because it says it cannot load the base class. I tried to define another class BaseUserControl, non generic, hoping it would load it, but the trick doesn't seem to work.
它工作正常,但不能在 VisualStudio 设计器中编辑 MyControl,因为它说它不能加载基类。我试图定义另一个非通用类 BaseUserControl,希望它能加载它,但这个技巧似乎不起作用。
I already have a workaround: define an interface, IMyInterface<T>, and then create my control as
我已经有了一个解决方法:定义一个接口 IMyInterface<T>,然后将我的控件创建为
public partial class MyControl : UserControl, IMyInterface<SomeClass>
But I lose my base virtual methods (not a big deal, but still...).
但是我丢失了我的基本虚拟方法(没什么大不了的,但仍然......)。
Is there a way to create a base generic class for a UserControl, with the possiblity to edit it in the VisualStudio Designer?
有没有办法为 UserControl 创建一个基本的通用类,并可以在 VisualStudio 设计器中对其进行编辑?
采纳答案by bernhardrusch
We're doing the same thing and we work around by specializing a class first and derive from the specialized class. Using the code from your example this means something like:
我们正在做同样的事情,我们通过首先专门化一个类并从专门化的类派生来解决。使用您示例中的代码,这意味着:
public partial class UserControl : UserControlDesignable
{
...
}
public class UserControlDesignable : BaseUserControl<Someclass> { }
The designer is still acting flaky sometimes - but most of the time it works.
设计师有时仍然表现得很古怪——但大多数时候它是有效的。
回答by Frederik Gheysels
You'll have to trick the designer by adding a 'regular' class that inherits from your generic base form. Your designable form should then inherit from this class. The following 2 class definitions are thus in the same file. You'll have to make sure that the class that inherits from the generic base user-control, is the last class in the file.
你必须通过添加一个从你的通用基本形式继承的“常规”类来欺骗设计者。然后您的可设计表单应该从这个类继承。因此,以下 2 个类定义在同一个文件中。您必须确保从通用基本用户控件继承的类是文件中的最后一个类。
public MyForm : EditableCustomerForm
{}
public EditableCustomerForm : GenericForm<Customer>
{}
The designer will display the first class in the code file that it encounters.
设计器将显示它遇到的代码文件中的第一个类。
回答by yoel halb
Well this appears to be a bug in Visual studio.
好吧,这似乎是 Visual Studio 中的一个错误。
By digging into the framework (actually by adding a RootDesignerSerializer
with a custom type derived from CodeDomSerializer
and overriding the serialize
method), I was able to prove that the VS Code Dom provider is actually parsing wrong the generic classes, and instead of considering it as a generic class it is considering it as a regular class with the name class<T>
, which Type.GetType()
can of course not find.
通过深入研究框架(实际上是通过添加一个RootDesignerSerializer
派生自CodeDomSerializer
并覆盖该serialize
方法的自定义类型),我能够证明 VS Code Dom 提供程序实际上解析了错误的泛型类,而不是将其视为泛型类它正在考虑与名称的普通班class<T>
,这Type.GetType()
当然可以找不到。
I am still searching for a way to work around it, but in the mean time one can use the solutions above.
我仍在寻找一种解决方法,但同时可以使用上述解决方案。
There is a Microsoft.Connect bug report on it, please vote on it at https://connect.microsoft.com/VisualStudio/feedback/details/797279/win-forms-designer-error-when-inheriting-from-a-generic-form
有一个关于它的 Microsoft.Connect 错误报告,请在https://connect.microsoft.com/VisualStudio/feedback/details/797279/win-forms-designer-error-when-inheriting-from-a-上投票通用形式