C#Winforms Designer调用基本属性而不是“新"属性? [英] C# Winforms Designer calling base property instead of 'new' one?
问题描述
我有一个继承自另一个类的类,该类具有非虚拟属性(控件").
I have a class that inherits from another class, who has a non-virtual property ("Controls").
由于我无法覆盖此属性,因此我将使用与我的属性相关联的'new'关键字.
Since I can't overwrite this property, Im using the 'new' keyword associated with my property.
在运行时,在正确的上下文中按需要调用此属性.
At runtime, this property is called as I want it to, in the correct context.
当我从设计器中打开表单时,设计器将调用base.Controls而不是"new"控件.
When I open my form from the designer, the designer calls the base.Controls instead of my 'new' control.
我是否缺少某些东西,或者这只是Winforms设计器中的错误行为?
Am I missing something, or is this just incorrect behavior in the winforms designer?
编辑,添加了有问题的代码以获取更多说明.我有以下课程:
Edit, added the code in question for more explanation. I have the following class:
public partial class BauerGroupBox : ComponentFactory.Krypton.Toolkit.KryptonGroupBox
{
public BauerGroupBox()
: base()
{
}
public new Control.ControlCollection Controls
{
get
{
MessageBox.Show("GOT THERE");
return this.Panel.Controls;
}
}
}
当我在Intialize组件中获得以下代码时:
When I get to the following code in my intializecomponent:
BauerGroupBox thisBox = new BauerGroupBox()
thisBox.Controls.Add(something)
当我在代码中添加新的'BauerGroupBox'时,它可以正常工作.但是,当我在设计器中打开代码时(即使在调试devenv时),也不会显示消息框,也不会出现断点.
When I add a new 'BauerGroupBox' to my code, it works fine. However, when I open my code in the designer (even while using the debugging the devenv), the messagebox is NOT shown, and the breakpoint is NOT hit.
当我运行我的应用程序时,命中了断点.
When I run my app, the breakpoint is hit.
推荐答案
您遗漏了一些东西-您所描述的是正确的行为.
You're missing something - what you're describing is correct behaviour.
解决方法是在调用InitializeComponent()
之后立即重新填充新的Controls
属性.像这样:
The workaround is to re-populate your new Controls
property right after the call to InitializeComponent()
. Like so:
public MyForm() {
InitializeComponent();
this.Controls.AddRange( base.Controls );
}
但是,为什么要尝试覆盖" Controls
属性?您追求什么新的非标准行为?我相信还有更好的选择.
However, why are you trying to "override" the Controls
property? What new, non-standard, behaviour are you after? I believe there is a better alternative.
这篇关于C#Winforms Designer调用基本属性而不是“新"属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!