Windows 窗体用户控件设计时属性

发布于 2024-10-08 18:55:13 字数 733 浏览 5 评论 0原文

我正在与用户控件作斗争。我有一个代表寻呼机的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦年海沫深 2024-10-15 18:55:13

当 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.

格子衫的從容 2024-10-15 18:55:13

因此,包含 UserControl 的 Windows 窗体设计器抛出异常:

此错误的实例 (1)

  1. Infrastructure.BaseModule.MYWindow.Designer.cs Line:108 Column:1 Hide Call Stack

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)

  1. Infrastructure.BaseModule.MYWindow.Designer.cs Line:108 Column:1 Hide Call Stack

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文