如何获得Windows窗体设计器以使用外部程序集中的资源? [英] How do I get the Windows Forms Designer to use resources from external assembly?

查看:103
本文介绍了如何获得Windows窗体设计器以使用外部程序集中的资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows Forms项目中的控件上使用的资源文件中有一些资源(在这种情况下为图像).除非它们位于特定位置,否则Visual Studio资源选择"对话框对从资源文件中选择图像没有很好的支持,但是您可以直接编辑设计器文件,这很好.该应用程序可以编译并正确运行,并且Windows Forms Designer足够聪明,不会弄乱我手工编辑的代码.

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;

现在,我想将这些资源移动到外部程序集中,以便它们可以被多个应用程序使用.我可以设置一个程序集来毫无问题地公开其资源(只要我使用的是Visual Studio 2008或更高版本),就可以正常工作.当我更改设计器代码以从新位置引用图像时,该代码将编译并正确运行,但是现在Windows Forms Designer会在生成代码时更改我的代码.它将图像的二进制文件嵌入本地资源文件中,并从那里引用它.

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;

Windows窗体设计器将

更改为:

is changed by the Windows Forms Designer to:

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

Windows窗体设计器似乎了解从同一程序集中而不是从外部提取资源.有什么方法可以让Windows Forms Designer允许我使用外部程序集中的资源?

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?

推荐答案

不,设计器不支持此功能.重要的是它的工作方式不一样,否则通过卫星程序集进行本地化将无法正常工作.

Nope, the designer doesn't support this. Important that it works the way it does, localization through satellite assemblies wouldn't work otherwise.

您可以执行此操作,但是您必须自己编写代码.您可以在Resources.Designer.cs文件中找到几乎所有内容.请考虑是否值得这样做,它不是非常可维护的,并且共享资源程序集并不是什么优化.一个TB的磁盘不到一百美元.

You can do this but you have to write the code yourself. Pretty much what you find in the Resources.Designer.cs file. Do consider if this is worth the effort, it isn't very maintainable and sharing resource assemblies isn't much of an optimization. A terabyte disk is less than a hundred bucks.

顺便说一句:从不自己编辑Resources.Designer.cs文件.

Btw: never edit the Resources.Designer.cs file yourself.

这篇关于如何获得Windows窗体设计器以使用外部程序集中的资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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