VS2010: How to avoid Windows Forms designer problems when working with inherited user controls?

社会主义新天地 提交于 2019-12-06 01:27:18

问题


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()
  {
  }
}


来源:https://stackoverflow.com/questions/5405986/vs2010-how-to-avoid-windows-forms-designer-problems-when-working-with-inherited

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!