C# 控件未加载到C中的设计器中#

C# 控件未加载到C中的设计器中#,c#,winforms,user-controls,designer,C#,Winforms,User Controls,Designer,一个非常简单的例子。我有两门课: public class BaseControl : UserControl { } public partial class MyControl : BaseControl { public MyControl() { InitializeComponent(); } } 我可以在设计器模式下打开BaseControl,但MyControl不能(VS 2008)。我不能仅仅从类UserControl继承类My

一个非常简单的例子。我有两门课:

public class BaseControl : UserControl  {  }

public partial class MyControl : BaseControl 
{
    public MyControl()
    {
        InitializeComponent();
    } 
}

我可以在设计器模式下打开BaseControl,但MyControl不能(VS 2008)。我不能仅仅从类UserControl继承类MyControl。有什么方法可以解决这个问题吗?

请确保以这种方式定义了类:

public class BaseControl : UserControl  
{  
    public BaseControl()
    {
        InitializeComponent();
    } 
}

public partial class MyControl : BaseControl 
{
    public MyControl()
    {
        InitializeComponent();
    } 
}

确保已按以下方式定义了类:

public class BaseControl : UserControl  
{  
    public BaseControl()
    {
        InitializeComponent();
    } 
}

public partial class MyControl : BaseControl 
{
    public MyControl()
    {
        InitializeComponent();
    } 
}

您需要使用
[Designer(typeof(MyControlDesigner))]
属性装饰类MyControl


并编写继承了
IDesigner的
MyControlDesigner

您需要使用
[Designer(typeof(MyControlDesigner))]属性装饰类MyControl


然后编写继承了
IDesigner

MyControlDesigner
类,我找到了解决方案。VS设计器需要基类中的InitializeComponent方法(在我的例子中是BaseControl)。我找到了解决方案。VS designer需要基类中的InitializeComponent方法(在我的例子中是BaseControl)。设计器生成
InitializeComponent()
方法,并实际创建控件、设置属性并将它们添加到UserControl上的控件集合中。
InitializeComponent()
方法由设计器生成,并实际创建控件、设置属性并将它们添加到UserControl上的控件集合中。