如何让 Windows 窗体设计器使用外部程序集中的资源?
我在资源文件中有一些资源(在本例中为图像),我在 Windows 窗体项目中的控件上使用这些资源。 Visual Studio 资源选择对话框没有很好地支持从资源文件中选择图像,除非它们位于特定位置,但您可以直接编辑设计器文件,这工作得很好;应用程序可以正确编译和运行,并且 Windows 窗体设计器足够智能,不会弄乱我手工编辑的代码。
// in an assembly named ResourceConsumer
this.button1.Image = global::ResourceConsumer.Properties.Resources.Close32x32;
现在我想将这些资源移动到外部程序集,以便多个应用程序可以使用它们。我可以设置一个程序集来公开其资源,而不会出现任何问题(只要我使用 Visual Studio 2008 或更高版本),并且效果很好。当我更改设计器代码以从新位置引用图像时,代码可以正确编译并运行,但现在 Windows 窗体设计器只要生成代码就会更改我的代码;它将图像的二进制文件嵌入到本地资源文件中并从那里引用它。
// ResourceProducer is an external assembly containing resources
this.button1.Image = global::ResourceProducer.Properties.Resources.Exit32x32;
Windows 窗体设计器将其更改为:
this.button1.Image = ((System.Drawing.Image)(resources.GetObject("button1.Image")));
Windows 窗体设计器似乎理解从同一程序集中提取资源,但不理解外部资源。有什么方法可以让 Windows 窗体设计器允许我使用外部程序集中的资源吗?
I have some resources (images in this case) in a resource file that I use on controls in my Windows Forms project. The Visual Studio Resource Selection dialog doesn't have very good support for choosing images from resource files unless they're in specific locations, but you can edit the designer file directly, and this works just fine; the application compiles and runs correctly, and the Windows Forms Designer is smart enough to not mess up my hand-edited code.
// in an assembly named ResourceConsumer
this.button1.Image = global::ResourceConsumer.Properties.Resources.Close32x32;
Now I want to move those resources to an external assembly so they can be used by multiple applications. I can set up an assembly to expose its resources without a problem (as long as I'm using Visual Studio 2008 or later), and this works fine. When I change the designer code to reference the image from its new location, the code compiles and runs correctly, but now the Windows Forms Designer changes my code whenever it generates code; it embeds the binary of the image in the local resource file and references it from there.
// ResourceProducer is an external assembly containing resources
this.button1.Image = global::ResourceProducer.Properties.Resources.Exit32x32;
is changed by the Windows Forms Designer to:
this.button1.Image = ((System.Drawing.Image)(resources.GetObject("button1.Image")));
The Windows Forms Designer seems to understand pulling a resource from within the same assembly, but not an external one. Is there any way to have the Windows Forms Designer allow me to use a resource from an external assembly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

发布评论
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我有完全相同的同样的问题 并且可能有另一种方法,具体取决于您的代码库。如果已修改为引用特定资源的属性位于自定义控件上,则您可以将
[[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
属性添加到这些属性(如果需要,覆盖或隐藏它们) ),设计师不会打扰他们。但是,如果属性位于基本 UI 元素(例如 Form.BackgroundImage)上,那么您还必须覆盖或隐藏这些属性,我不确定这是否理想。
I have the exact same problem and there may be an alternative approach, depending on your code-base. If the properties that have been modified to reference a specific resource are on custom controls, then you could add the
[[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
attribute to those properties (override or hide them, if necessary) and the Designer will leave them alone.However, if the properties are on basic UI elements (e.g. Form.BackgroundImage) then you'd have to have to override or hide those as well and I'm not sure how desirable that is.