从多个.RESX文件生成强类型资源类 [英] Generating a Strongly Typed Resource Class From Multiple .RESX files

查看:131
本文介绍了从多个.RESX文件生成强类型资源类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我最近一直试图为我的项目生成强类型资源文件。



我有多个.resx文件,到目前为止我发现的所有文档只暗示你只能从一个XML或文本文件中生成一个类。或者至少我理解文档这里



我继承了一个项目,需要更改命名空间,因此需要重新生成现有的Resource.Designer.cs。



在此先感谢任何建议,我一直坚持这一点。



拉里

Hi All,

I've been stuck recently trying to generate a strongly typed resource file for my project.

I have multiple ".resx" files and all documentation I've found so far only implies you can only generate a class from a single XML or text file. Or at least that is how I understand the documentation here.

I have inherited a project and have needed to change the namespace, hence the existing Resource.Designer.cs needs to be regenerated.

Thanks in advance for any suggestions, I've been really stuck on this one.

Larry

推荐答案

这根本不是真的。您可以自动生成许多不同的强类型资源。您只需要一个文件(不,不仅是文本文件,支持许多文件类型),并为此文件指定正确的名称。现在,资源将始终成功创建,但在某些情况下,Visual Studio将生成强类型资源,在其他情况下,生成的资源将作为字节数组进行访问。这取决于文件类型;如果系统熟悉此类型,则生成的资源将被强类型化。如果没有,您将不得不处理字节数组。通常,您将它放在内存流中并使用您自己类型的构造函数读取流,该构造函数知道如何处理相关资源的类型。



让我举个例子说明PNG图像文件。您应该同意这不是文本文件。您应该使用有效的PNG内容(最重要的项目,显然是:-))准备此类文件,并以适当的方式命名,例如MyImage.PNG。创建一个空的* .resx项,单击添加现有文件(大多数问题中的最佳选项;特别是,尽量避免在Visual Studio中创建图像:它会强制您使用其编辑器,这是丑陋的;它赢了' t允许您独立使用资源编辑文件)。当添加文件时,它会将它的副本添加到项目中(具有适当的属性,因此文件不会被复制到输出目录),在资源文件中引用此文件并注意! - 将创建一个自动生成的C#文件



此自动生成的文件将作为子节点放置到资源文件节点,如解决方案资源管理器中所示。在编辑器中打开此文件。在此文件中,您将找到名为 MyImage 的静态属性(在某些其他情况下,您将获得名称为 close 的属性您的输入文件)。此属性将强类型为图像。底层代码将从资源和读取以及图像对象中提取它,因此您不需要自己做这样的事情。只需在项目中使用此属性作为一些即用型图像。当您编辑文件MyImage.PNG并重建项目时,嵌入的图像将被正确更新。



这就是全部。至于资源中使用的其他文件类型 - 请参见上文。



-SA
This is not true at all. You can generate a lot of different strongly-typed resources automatically. You just need to have a file (no, not only a text file, many file types are supported) and give this file proper name. Now, the resource will always be successfully created, but in some cases Visual Studio will generate a strongly typed resource, in other cases, the generated resource will be accessible as an array of bytes. It depends on the file type; if the system is familiar with this type, the generated resource will be strongly typed. If not, you will have to deal with the array of bytes. Typically, you put it in a memory stream and read the stream with your own constructor of your own type which "knows" how to deal with the type of resource in question.

Let me give you an example of, say, PNG image file. You should agree that this is not a text file. You should prepare such file with valid PNG content (most important item, apparently :-)), and name it in a proper way, say, "MyImage.PNG". Create an empty *.resx item, click "Add existing file" (the best option in most question; in particular, try to avoid creation of images in Visual Studio: it will force you using its editor, which is ugly; it won't allow you to edit files using in resources independently). When the file is added, it will add a copy of it to the project (with proper properties, so the file won't be copied to output directory), a reference to this file in the resource file and — attention! — will create an auto-generated C# file.

This auto-generated file will be placed as a child node to your resource file node, as presented in the Solution Explorer. Open this file in the editor. In this file, you will find the static property named MyImage (in some other cases, you will get a property with the name close to the name of your input file). This property will be strongly typed as image. The underlying code will extract it from resource and read and the image object, so you don't need to do anything like that by yourself. Just use this property in your project as some ready-to-use image. When you edit your file "MyImage.PNG" and rebuild your project, the embedded image will be properly updated.

That's all. As to other file types to be uses in resource — please see above.

—SA


这篇关于从多个.RESX文件生成强类型资源类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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