简体   繁体   English

C# WinForms Base UserControl

[英]C# WinForms Base UserControl

I'm having some problems in VS2019 with UserControls.我在使用 UserControls 的 VS2019 中遇到了一些问题。

I have a base user control (WinForms) that I want all my other User Controls to be inherited from.我有一个基本用户控件 (WinForms),我希望从中继承所有其他用户控件。 THe base control provides a number of common properties that I need.基本控件提供了许多我需要的常用属性。

public partial class MyBaseControl : System.Windows.Forms.UserControl
{

    internal Int32 _caseID;
    internal object _objectID;

    public int CaseID { get => _caseID; set => _caseID = value; }
    public object ObjectID { get => _objectID; set => _objectID = value; }

    internal virtual void MakeScreenReadOnly()
    {
    }
    public MyBaseControl()
    {
        Type systemType = this.GetType();
        PropertyInfo propertyInfo = systemType.GetProperty("DoubleBuffered", BindingFlags.Instance | BindingFlags.NonPublic);
        this.AutoScroll = true;

    }

}

The controls that inherit this base control all work fine when the application runs, except I can't view the control in the designer.继承此基本控件的控件在应用程序运行时都可以正常工作,但我无法在设计器中查看该控件。 The user control is用户控件是

public partial class AddressControl : MyBaseControl
    {
....
}

I get the error我得到错误

The service System.Windows.Forms.Design.IEventHandlerService already exists in the service container. Parameter name: serviceType

when trying to view AddressControl in the designer.尝试在设计器中查看 AddressControl 时。

This used to work, in earlier version of Visual Studio.这曾经在早期版本的 Visual Studio 中工作。 Does anyone have any ideas?有没有人有任何想法?

  • Quit from Visual Studio.从 Visual Studio 退出。
  • Open File Explorer: navigate to the folder that contains your project.打开文件资源管理器:导航到包含您的项目的文件夹。
  • Delete the “bin” and “obj” folders.删除“bin”和“obj”文件夹。
  • Open Visual Studio again and open and recompile the project.再次打开 Visual Studio 并打开并重新编译项目。

You can try to recreate the user control.您可以尝试重新创建用户控件。

Exclude the old control from the project to keep it on disk.从项目中排除旧控件以将其保留在磁盘上。

Move the old files outside the project folder ( MyBaseControl.cs & MyBaseControl.Designer.cs ).将旧文件移出项目文件夹( MyBaseControl.csMyBaseControl.Designer.cs )。

Use the Project > Add > User control wizard.使用Project > Add > User control向导。

Copy your old code in it.将您的旧代码复制到其中。

And don't remove the InitializeComponent();并且不要删除InitializeComponent(); from the constructor: add your code after.从构造函数:之后添加您的代码。

It may work and solve your problem if Visual Studio is not corrupted else you can try repair or to uninstall and reinstall it.如果 Visual Studio 没有损坏,它可能会起作用并解决您的问题,否则您可以尝试修复或卸载并重新安装它。

Here what I tried:这是我尝试过的:

  • I added this usercontrol:我添加了这个用户控件:

     public partial class UserControl1: UserControl { public UserControl1() { InitializeComponent(); } }
  • I compile.我编译。

  • I added another usercontrol and change the parent:我添加了另一个用户控件并更改了父控件:

     public partial class UserControl2: UserControl1 { public UserControl2() { InitializeComponent(); } }

It works fine and the designer display both correctly.它工作正常,设计师正确显示。

Next I added your code while keeping the InitializeComponent() call :接下来,我在保持InitializeComponent()调用的同时添加了您的代码:

public partial class UserControl1 : UserControl
{
  internal Int32 _caseID;
  internal object _objectID;

  public int CaseID { get => _caseID; set => _caseID = value; }
  public object ObjectID { get => _objectID; set => _objectID = value; }

  internal virtual void MakeScreenReadOnly()
  {
  }
  public UserControl1()
  {
    InitializeComponent();
    Type systemType = this.GetType();
    PropertyInfo propertyInfo = systemType.GetProperty("DoubleBuffered", BindingFlags.Instance | BindingFlags.NonPublic);
    this.AutoScroll = true;
  }
}

I compile.我编译。

All works fine too.一切正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM