简体   繁体   English

在 .designer.cs 中控制创建

[英]Control creation in .designer.cs

I have some user controls in our app that refer back to the form they're sitting on to get some initialisation values.我在我们的应用程序中有一些用户控件,它们引用回他们所在的表单以获取一些初始化值。 Depending on the form they're on different behaviours are needed.根据不同的形式,他们需要不同的行为。

So the user controls catch OnLoad and ParentChanged events, and you FindForm to gain access to the form they're sat on... and all has been good for a couple of years.因此,用户控件会捕获 OnLoad 和 ParentChanged 事件,并且您可以通过 FindForm 来访问它们所在的表单……而且这一切已经好几年了。

However, last week we started to have problems with some controls not initialising properly, and after much searching I can see that what's happening is that if the controls are sat on a container rather than directly on the form, then when the ParentChanged event of the control fires, it could be that the container is not yet added to the form and so FindForm does not work.然而,上周我们开始遇到一些控件没有正确初始化的问题,经过多次搜索,我可以看到发生的事情是,如果控件位于容器上而不是直接位于窗体上,那么当控件触发,可能是容器尚未添加到表单中,因此 FindForm 不起作用。

The odd thing is that this has only just started happening, and it seems that since we upgraded to the latest VS, when a form is saved after editing, the designer is massively changing the order in which controls are created and sometimes this causes a fail.奇怪的是,这才刚刚开始发生,似乎自从我们升级到最新的 VS,当表单在编辑后保存时,设计者正在大量更改控件的创建顺序,有时这会导致失败.

Does anyone know if/why the VS designer behaviour has changed?有谁知道 VS 设计师的行为是否/为什么发生了变化? And also, if there is anyway to control the create order of form controls in the designer file?而且,如果无论如何要控制设计器文件中表单控件的创建顺序?

I can manually edit the designer file to fix, but on a resave, VS puts it back the way it wants again.我可以手动编辑设计器文件进行修复,但是在重新保存时,VS 会再次按照它想要的方式将它放回原处。

So while this might have worked for you for some time, the core problem lies in the handy message included in every Designer.cs file.因此,虽然这可能对您有用一段时间,但核心问题在于每个 Designer.cs 文件中包含的方便消息。

  /// <summary>
  /// Required method for Designer support - do not modify
  /// the contents of this method with the code editor.
  /// </summary>

In reality this message should really apply to anything in the Designer.cs file.实际上,这条消息应该真正适用于 Designer.cs 文件中的任何内容。 I would gently suggest that you should take a hard look at your code and why you're doing what you're doing, and change it as soon as possible to not rely on Designer.cs file modifications.我会温和地建议您仔细查看您的代码以及您为什么要这样做,并尽快对其进行更改,以免依赖 Designer.cs 文件修改。

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

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