[英]C# WinForms Base UserControl
我在使用 UserControls 的 VS2019 中遇到了一些问题。
我有一个基本用户控件 (WinForms),我希望从中继承所有其他用户控件。 基本控件提供了许多我需要的常用属性。
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;
}
}
继承此基本控件的控件在应用程序运行时都可以正常工作,但我无法在设计器中查看该控件。 用户控件是
public partial class AddressControl : MyBaseControl
{
....
}
我得到错误
The service System.Windows.Forms.Design.IEventHandlerService already exists in the service container. Parameter name: serviceType
尝试在设计器中查看 AddressControl 时。
这曾经在早期版本的 Visual Studio 中工作。 有没有人有任何想法?
您可以尝试重新创建用户控件。
从项目中排除旧控件以将其保留在磁盘上。
将旧文件移出项目文件夹( MyBaseControl.cs
和MyBaseControl.Designer.cs
)。
使用Project > Add > User control
向导。
将您的旧代码复制到其中。
并且不要删除InitializeComponent();
从构造函数:之后添加您的代码。
如果 Visual Studio 没有损坏,它可能会起作用并解决您的问题,否则您可以尝试修复或卸载并重新安装它。
这是我尝试过的:
我添加了这个用户控件:
public partial class UserControl1: UserControl { public UserControl1() { InitializeComponent(); } }
我编译。
我添加了另一个用户控件并更改了父控件:
public partial class UserControl2: UserControl1 { public UserControl2() { InitializeComponent(); } }
它工作正常,设计师正确显示。
接下来,我在保持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;
}
}
我编译。
一切正常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.