设计器强制将资源嵌入表单中,而不是使用Resources.resx? [英] Designer forcing resources to be embedded in form, rather than using Resources.resx?

查看:82
本文介绍了设计器强制将资源嵌入表单中,而不是使用Resources.resx?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题让我挠头!我会尽量保持简洁。

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

高层次:

问题是,尽管该项目运行正常,并且代码对我来说看起来不错。每当我编辑和构建某些表单时,Visual Studio都会以非常不可取的方式重写 *。Designer.cs 文件。

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.

我非常有信心这些 *。Designer.cs 文件没有被编辑过(尤其是自动生成的部分

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

更多信息:

我们的项目使用自定义控件,其中一些控件继承自 PictureBox 。在存在这些控件的表单上,如果查看 *。Designer.cs 文件,我要么会看到 Image 属性未设置,或者 Image 属性引用存储在项目的resx文件中的图像,如下所示。

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;

但是,如果我只是简单地通过添加另一个控件来修改此表单(将另一个按钮拖动到表单上),生成项目后,Visual Studio会广泛编辑 MyForm.Designer.cs MyForm.resx 文件,甚至对于现有文件控件上未被触摸的窗体。似乎它将控件所需的所有图像都嵌入到 MyForm.resx 文件中,然后在 MyForm.Designer.cs <中引用它们。 / code>如下:

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")));

这显然不是我想要的。为什么Visual Studio希望表单现在使用本地资源,而不是嵌入在项目的 Resources.resx 文件中的本地资源,因为它很高兴在修改表单之前?如果我去设计者,请查看 customButton 的属性,然后尝试将Image属性设置为项目资源文件中的图像,它允许它,但是在下一步,它将立即恢复为MyForm.resx中嵌入的本地引用。

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.

为什么会这样?

推荐答案

另一个问题

基本上,即使我看到了一些建议将您的应用程序资源集中在一个程序集中,这似乎是一个不好的选择。 VS Designer只是不喜欢访问当前程序集外部的资源,尽管这样做可以,但是它也会更改您的代码以通过将它们嵌入来将这些资源带入当前程序集中,从而阻碍了将资源保留在其中的努力。

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.

这篇关于设计器强制将资源嵌入表单中,而不是使用Resources.resx?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆