代码之家  ›  专栏  ›  技术社区  ›  Brian A. Henning

从外部创建的访问资源。resx文件

  •  0
  • Brian A. Henning  · 技术社区  · 7 年前

    我的主Windows窗体(托管C++)项目有一个类,该类提供了一个图像,其中包含可以显示或隐藏的图块,以创建响应图。

    using(ResXResourceWriter resx = new ResXResourceWriter(sfd.FileName)) {
        resx.AddResource("Size", canvas.Size);
        List<int> IDs = canvas.IDs;
        resx.AddResource("IDList", IDs);
        resx.AddResource("BackgroundIndex", canvas.BackgroundIndex);
        foreach(int id in IDs) {
            String positionKey = String.Format("Position.id{0}", id);
            String visibilityKey = String.Format("Visibility.id{0}", id);
            String imageKey = String.Format("Image.id{0}", id);
            resx.AddResource(imageKey, canvas.TileImage(id));
            resx.AddResource(positionKey, canvas.TilePosition(id));
            resx.AddResource(visibilityKey, canvas.TileVisible(id));
        }
    }
    

    我可以打开。在文本编辑器中输入resx文件,并查看其格式是否正确,是否包含预期的数据。

    然后我接受了。并将其添加到我的主应用程序的项目中。现在我不知道如何获取其中的资源。我试过的代码是:

    ResourceManager ^ image_rm = gcnew ResourceManager(
        "resx_file_name_without_extension", GetType()->Assembly);
    ResourceSet ^ image_rs = image_rm->GetResourceSet(
        System::Globalization::CultureInfo::CurrentCulture, true, true);
    

    在运行时,第二行(即 GetResourceSet System.Resources.MissingManifestResourceException 带有以下消息文本:

    找不到适合指定区域性或 中立文化。确保“resx\u file\u name\u without\u extension.resources”为 在编译时正确嵌入或链接到程序集“my_assembly”,或

    我怀疑我的问题是。。。我真的不知道。也许我没有使用正确的标识符 ResourceManager 构造函数。我尝试明确设置“从构建中排除: “和”内容: “在文件的属性中,但这没有任何影响。

    1 回复  |  直到 7 年前
        1
  •  0
  •   Brian A. Henning    7 年前

    结果是评论 this unanswered question 是秘制酱汁。将根命名空间前置到标识符使 ResourceManager 快乐:

    ResourceManager ^ image_rm = gcnew ResourceManager(
        "my_root_namespace.resx_file_name_without_extension", GetType()->Assembly);
    ResourceSet ^ image_rs = image_rm->GetResourceSet(
        System::Globalization::CultureInfo::CurrentCulture, true, true);
    

    编译器如何或为什么决定将资源放置在该名称空间中,对我来说仍然有点神秘,但这是另一天的琐事。