Designer forcing resources to be embedded in form, rather than using Resources.resx?

做~自己de王妃 提交于 2020-01-04 04:34:24

问题


This problem has left me scratching my head! I'll try to be as concise as possible.

On a high level:

The problem is that although the project works fine and the code looks good to me. Whenever I edit and build certain forms, Visual Studio re-writes the *.Designer.cs files in a way that is very undesirable.

I quite confident that these *.Designer.cs files have not been edited (especially the auto-generated portion) in the past.

In more detail:

Our project uses custom controls, some which inherit from PictureBox. On the forms where these controls are present, if I view the *.Designer.cs file, I either see that the Image property is not set, or the Image property refers to an image stored in the project's resx file like below, which is all well and good.

this.customButton.Image = global::MyProject.Properties.Resources.buttonImage;

However, if I simply modify this form by adding another control (drag another button onto the form) and build the project, Visual Studio extensively edits the MyForm.Designer.cs and MyForm.resx files, even for the existing controls on the form that were not touched. It seems that it embeds all the images needed by the controls in the MyForm.resx file and then refers to them in the MyForm.Designer.cs as follows:

System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MyForm));
this.customButton.Image = ((System.Drawing.Image)(resources.GetObject("customButton.Image")));

This is obviously not what I want. Why does Visual Studio want the form to use a local resource now, instead of the one embedded in the project's Resources.resx file as it was happy to do before the form was modified? If I go to the designer, view the properties of the customButton, and try to set the Image property to the image in the project resource file, it allows it, but on the next click, it will immediately revert back to the local reference embedded in MyForm.resx.

Any ideas why this is happening?


回答1:


I figured this out with the help of another question

Basically, even though I've seen several recommendations to centralize your application's resources in a single assembly, this appears to be a bad choice. The VS Designer just doesn't like having to access resources external to the current assembly and while it can do so, it will also change your code to bring those resources in the current assembly by embedding them, thwarting your efforts to keep the resources in a single assembly.

I basically had to go back to keeping resources in the assembly the uses them.



来源:https://stackoverflow.com/questions/7844153/designer-forcing-resources-to-be-embedded-in-form-rather-than-using-resources-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!