资源管理器和资源编写器的问题 [英] Problems with Resource Manager and Resource Writer

查看:98
本文介绍了资源管理器和资源编写器的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!
我是一名初学者程序员,我想创建这样的东西->
我想要一个包含图像及其描述的程序(用户可以添加图像或描述).因此,我想将程序的数据存储在一个文件中.我想使用如果获取一些新数据将构建的资源.示例代码如下:



创建资源:

Hi!
I am a beginner programmer and I want to create something like this->
I want a program that contains images and their description (user can add image or/and description). Therefore I want to store program’s data somewhere in one file. I wanted to use Resources that would be built if some new data is acquired. The example code is below:



creating resources:

try
{
    ResourceWriter rw = new ResourceWriter("xxx.dll");
    rw.AddResource("o", Resource1.STALMEt);
    rw.Generate();
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}



并且比恢复:



and than restoring:

try
           {
               ResourceManager rm = new ResourceManager("xxx.dll", Assembly.GetExecutingAssembly());
               Image a = (Image)rm.GetObject("o");

           }
           catch (Exception es)
           {
               MessageBox.Show(es.Message);
           }



我收到此错误:
找不到适合于指定区域性或中性区域性的任何资源.确保在编译时将"xxx.dll.resources"正确地嵌入或链接到程序集"Resorce_TEST"中,或者确保所需的所有附属程序集都可加载并经过完全签名.

//Resorce_Test是解决方案名称.
我不明白这个错误,为什么不起作用.我真的可以帮忙.
非常感谢您的帮助.



I get this error:
Could not find any resources appropriate for the specified culture or the neutral culture. Make sure "xxx.dll.resources" was correctly embedded or linked into assembly "Resorce_TEST" at compile time, or that all the satellite assemblies required are loadable and fully signed.

//Resorce_Test is solution name.
I don`t understand this error and why isn`t this working. I could really use some help.
Thanks a lot for help.

推荐答案

似乎您只需要静态资源.那你就不需要这些了.<​​br/>
创建新的.RESX资源.添加一些字符串,图像文件等.在资源的项目节点下,Visual Studio将创建自动生成的C#代码文件.打开它.您会发现已添加的每个资源的静态声明.在代码中使用这些声明,就像常规的静态属性一样!


回答后续问题.

查看下面提供的自动生成的C#文件.让我们使用资源"STALMEt",它是一个位图.无需使用任何代码或资源管理器.您只需要使用静态类Resource1及其静态属性.顺便说一句,请始终使用明智的名称,将所有内容重命名为默认的"Resource1",不允许使用数字,请遵循Microsoft的命名约定.
考虑您正在以某种形式编写它:

It looks like you only need static resources. Then you don''t need all that.

Create new .RESX resource. Add some stings, image files, etc. Under the project node of your resource, Visual Studio will create auto-generated C# code file. Open it. You will find static declaration of every resource you have added. Use those declarations in you code like regular static properties!


Answering a follow-up Question.

Look at your auto-generated C# file you provided below. Let''s make use of the resource "STALMEt", which is a bitmap. No need to use any code or resource manager. You only need to use static class Resource1 and its static properties. By the way, always use sensible name, rename everything out of default "Resource1", allow no numeral, follow Microsoft naming conventions.
Consider you''re writing it in some form:

public partial class MyForm : Form {
    //...

    //that's all about using resource, nothing else:
    static Bitmap bmp = Resorce_TEST.Resource1.STALMEt;    
    //...

    //this is how you can use that bitmap, for example:
    protected override void OnPaint(PaintEventArgs e) {
       base.OnPaint(e);
       e.Graphics.DrawImage(bmp, new Point(10, 12)); //here you use it
    } //OnPaint

    //...
} //class MyForm



而已.现在清楚了吗?


有关用户数据,请参见持久性的另一个答案.

—SA



That''s it. It it clear now?


See another Answer of persistence for the user data.

—SA


正在回答第二个后续问题.

首先,您应该知道将用户数据保留在何处.这只是一种完全合法的方法:您需要使用按用户创建的特殊目录,或者创建和使用特殊目录的子目录.

您需要对参数使用System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)方法.请参见枚举类型Environment.SpecialFolder.LocalApplicationData的其他成员.另外,请参见方法System.Environment.GetFolderPath的两个重载.
现在,当您有一个目录来存储本地应用程序数据时,您可能想要创建一个特定于您的应用程序的子目录,以保留用户最后选择的数据.

第二个问题是如何持久化数据.图像文件具有其自己的加载/存储方法,因此您可以使用它们.您可能需要使用某种模式将所有数据集成到一个文件中.我强烈建议开发一组特殊的纯数据类(对象图),并将其全部保存在一个文件中(最有可能是XML).您可以将所有图像文件保留为单独的文件,这很方便,但是需要一些编码工作(唯一名称,覆盖或保留旧文件等).或者,您可以将每个图像文件嵌入到单个数据文件中(这非常大而且不可读;使用XML,您需要将二进制文件存储在Base64片段中).

我几乎不建议使用System.Runtime.Serialization.DataContractSerializer.这是最非侵入性和鲁棒性的解决方案:您不必使数据可序列化或以任何方式进行修改.您只需要向要持久化的类和成员添加属性[DataContract]和[DataMember]属性(请不要忘记在每个DataContract属性中指定世界唯一的XML名称空间.请参见. System.Runtime.Serialization.DataContractAttributeSystem.Runtime.Serialization.DataMemberAttribute.序列化程序甚至可以持久保存不是树的任何对象图(具有循环引用),这非常健壮.它的可支持性主要基于以下事实:如果您修改了自己的任何内容,如果数据类不是合同的一部分,则将读取您的持久化数据.如果您以增量方式迁移架构(例如,通过添加新的合同成员和类型,但不删除任何现有的合同成员和类型),则持久化架构将仍然可以读取.就像自动进行版本升级一样.请参阅 http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractserializer.aspx [
Answering second follow-up question.

First of all, you should know where to persist user data. This is only one fully legitimate way: you need to use special directory create per user, or create and use sub-directory of a special directory.

You need to use System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) method with the parameter. See other members of the enumeration type Environment.SpecialFolder.LocalApplicationData. Also, see two overloads of the method System.Environment.GetFolderPath.

Now when you have a directory to store your local application data, you may want to create a sub-directory specific to your application to persist the last data chosen by your user.

A second problem is how to persist data. Image files have their own load/store methods, so you can use them. You may need to integrate all your data in one file using some schema. I would highly recommend developing a special set of pure data classes (object graph) and persist it all in one file (most likely, XML). You can keep all image files as separate files, which is convenient but needs some coding effort (unique names, overwriting or keeping old files, etc.). Alternatively, you can embed each image file in your single data file (which is very bulky and not readable; with XML you will need to store binary files in Base64 fragments).

I would hardly recommend using System.Runtime.Serialization.DataContractSerializer. This is most non-intrusive and robust solution: you don''t have to make your data serializeable or modify in any way. You only need to add attributes [DataContract] and [DataMember] attributes to your classes and members you want to persist (don''t forget to specify you world-unique XML name space in each DataContract attribute. See System.Runtime.Serialization.DataContractAttribute and System.Runtime.Serialization.DataMemberAttribute. The serializer can even persist any object graph which is not a tree (with circular references), which is very robust. It''s supportability is mainly based on the fact that if you modify anything in your data classes which is not part of contract, you persisted data will be read. If you migrate your schema incrementally (let''s say, by adding new contract members and types but not removing any of the existing ones), your persisted schema will be read anyway. This is like automatic reconciliation of version upgrades. See http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractserializer.aspx[^].

—SA


这篇关于资源管理器和资源编写器的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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