VS2010: How to avoid Windows Forms designer proble

2019-04-07 14:33发布

问题:

Problem: The Windows Forms designer does not work for an inherited user control when the base class is implementing an interface from another assembly.

Platform: VS 2010 SP1, .NET 4.0 Framework

Error:

The designer could not be shown for this file because none of the classes within it can be designed. The designer inspected the following classes in the file: MyControl --- The base class 'MyBaseControlLib.MyBaseControl' could not be loaded. Ensure the assembly has been referenced and that all projects have been built.

at System.ComponentModel.Design.Serialization.CodeDomDesignerLoader.EnsureDocument(IDesignerSerializationManager manager) at System.ComponentModel.Design.Serialization.CodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager manager) at Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager serializationManager) at Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomDesignerLoader.DeferredLoadHandler.Microsoft.VisualStudio.TextManager.Interop.IVsTextBufferDataEvents.OnLoadCompleted(Int32 fReload)

I have a solution with 3 class library projects:

MyInterfaceLib:

namespace MyInterfaceLib
{
    public interface IMyInterface
    {
        void Foo();
    }
}

MyBaseControlLib:

namespace MyBaseControlLib
{
    using System.Windows.Forms;
    using MyInterfaceLib;

    public partial class MyBaseControl : UserControl, IMyInterface
    {
        public MyBaseControl()
        {
            InitializeComponent();
        }

        public void Foo()
        {
        }
    }
}

MyDerivedLib:

namespace MyDerivedControlLib
{
    using MyBaseControlLib;

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

Although the designer works for MyBaseControl it does not work for MyControl. If MyBaseControl does not implement IMyInterface, the designer also works for MyControl.

Any ideas?

Thanks, Robert

回答1:

We had the same issue. We used a workarround by creating a MyControlDesign class that is inherited by MyControl class.

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

public partial class MyControlDesign : MyBaseControl
{
  public MyControlDesign ():base()
  {
  }
}