Windows 窗体用户控件设计时属性
我正在与用户控件作斗争。我有一个代表寻呼机的 UserControl,它有一个以这种方式公开的 Presenter 对象属性:
[Browsable(false)]
[DesignSerializationAttribute(DesignSerializationAttribute.Hidden)]
public object Presenter { get; set; }
代码本身可以工作,因为我可以将控件拖放到 Windows From 中,而无需 Visual Studio 初始化此属性。 现在,因为在此控件的 Load 事件中,我调用了 Presenter 的一个方法,该方法在运行时为 null ...我引入了以下附加代码:
public override void OnLoad(...)
{
if (this.DesignMode)
{
base.OnLoad(e);
return;
}
presenter.OnViewReady();
}
现在,每次我打开包含此 UserControl 的窗口时,Visual Studio 都会修改Windows 设计器代码。所以,一旦我打开它,VS就会问我是否要保存它...当然,如果我向窗口添加一个控件,它不会保留更改...一旦我删除了UserControl Pager 问题消失... 我应该如何以正确的方式解决这个问题?我只是不希望演示者属性在设计时初始化,因为它是在运行时注入的......
I am struggling with a UserControl. I have a UserControl that represent a Pager and it has a Presenter object property exposed in this way:
[Browsable(false)]
[DesignSerializationAttribute(DesignSerializationAttribute.Hidden)]
public object Presenter { get; set; }
The code itself works as I can drag and drop a control into a Windows From without having Visual Studio initializing this property.
Now, because in the Load event of this control I call a method of the Presenter that at run-time is null ... I have introduced this additional code:
public override void OnLoad(...)
{
if (this.DesignMode)
{
base.OnLoad(e);
return;
}
presenter.OnViewReady();
}
Now, every time I open a Window that contains this UserControl, Visual Studio modifies the Windows designer code. So, as soon as I open it, VS ask me if I want to save it ... and of course, if I add a control to the Window, it doesn't keep the changes ... As soon as I remove the UserControl Pager the problem disappears ...
How should I tackle that in the proper way? I just don't want that the presenter property is initialized at design time as it is injected at runtime ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当 VS 尝试生成设计器代码时,您可能会遇到异常。
将 VS 的第二个副本附加到设计器,打开 Break On Exceptions,然后看看发生了什么。
You're probably getting an exception when VS tries to generate the designer code.
Attach a second copy of VS to the designer, turn on Break On Exceptions, and see what's going on.
因此,包含 UserControl 的 Windows 窗体设计器抛出异常:
此错误的实例 (1)
at System.ComponentModel.Design.Serialization .CodeDomSerializerBase.Error(IDesignerSerializationManager 管理器,字符串异常文本,字符串helpLink)
在 System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializeExpression(IDesignerSerializationManager 管理器,字符串名称,CodeExpression 表达式)
在 System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializeExpression(IDesignerSerializationManager 管理器,字符串名称,CodeExpression 表达式)
在 System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializeStatement(IDesignerSerializationManager 管理器,CodeStatement 语句)
So the exception is thrown by the Designer of the Windows form that contains the UserControl:
Instances of this error (1)
at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.Error(IDesignerSerializationManager manager, String exceptionText, String helpLink)
at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializeExpression(IDesignerSerializationManager manager, String name, CodeExpression expression)
at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializeExpression(IDesignerSerializationManager manager, String name, CodeExpression expression)
at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializeStatement(IDesignerSerializationManager manager, CodeStatement statement)