简体   繁体   English

VS2015中代码奇怪的变化

[英]Strange change of code in VS2015

I do not understand the behavior of Visual Studio 2015 Professional. 我不明白Visual Studio 2015 Professional的行为。 In a Winforms Designer I write this code: 在Winforms Designer中,我编写了以下代码:

this.btC.Image = dllres.Properties.Resources.find;

Unless, recompile and I find this other code: 除非,重新编译,我发现其他代码:

this.btC.Image = ((System.Drawing.Image)(resources.GetObject("btC.Image")));

Why is this happening? 为什么会这样?

The winforms designer internally uses something called Global Objects . winforms设计者在内部使用称为Global Objects东西。 Those work for, among other things, assigning project resources to image properties without converting them to a local form resource (in the yourForm.resx file). 除了其他方面,这些工作用于将项目资源分配给图像属性,而不将它们转换为本地表单资源(在yourForm.resx文件中)。

If you write that code by hand, the designer won't know it's a global object (global objects get assigned by the designer using the visual studio's IReferenceService service provider), and will just save the assigned image into the form's local resources, and change the code to what you are seeing upon serializing again. 如果您手动编写该代码,设计人员将不会知道它是一个全局对象(设计人员使用visual studio的IReferenceService服务提供商分配全局对象),并将分配的图像保存到表单的本地资源中,并更改再次序列化时所看到的代码。

So you either assign the resource using the designer (the resource picker dialog), or, as the comments in the generated ( .Designer.cs ) file say, do not modify that file, and assign it on the form's constructor, right after InitializeComponent() 因此,您可以使用设计器(资源选择器对话框)分配资源,或者,如生成的( .Designer.cs )文件中的注释所示,不要修改该文件,并在InitializeComponent()之后立即将其分配给表单的构造函数InitializeComponent()

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

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