设计者强制将资源嵌入到表单中,而不是使用 Resources.resx?
这个问题让我摸不着头脑!我会尽量简洁。
在高层次上:
问题是,尽管该项目运行良好并且代码对我来说看起来不错。每当我编辑和构建某些表单时,Visual Studio 都会以一种非常不可取的方式重写 *.Designer.cs
文件。
我非常有信心这些 *.Designer.cs
文件过去没有被编辑过(特别是自动生成的部分)。
更详细地说:
我们的项目使用自定义控件,其中一些控件继承自PictureBox
。在存在这些控件的窗体上,如果我查看 *.Designer.cs
文件,我要么看到 Image
属性未设置,要么看到 Image
属性指的是存储在项目的 resx 文件中的图像,如下所示,这一切都很好。
this.customButton.Image = global::MyProject.Properties.Resources.buttonImage;
但是,如果我只是通过添加另一个控件(将另一个按钮拖到表单上)来修改此表单并生成项目,则 Visual Studio 会广泛编辑 MyForm.Designer.cs
和 MyForm.resx< /code> 文件,即使对于表单上未触及的现有控件也是如此。看起来它把控件需要的所有图像嵌入到
MyForm.resx
文件中,然后在MyForm.Designer.cs
中引用它们,如下所示:
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 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在 另一个问题
基本上,尽管我已经看到了一些将应用程序的资源集中在单个程序集中的建议,但这似乎是一个糟糕的选择。 VS 设计器只是不喜欢访问当前程序集外部的资源,虽然它可以这样做,但它也会更改您的代码,通过嵌入这些资源将它们引入当前程序集中,从而阻碍您将资源保留在当前程序集中。单个组件。
我基本上不得不回去将资源保留在使用它们的程序集中。
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.