【问题标题】:ResourceMap not found error when referencing a resource file within a portable class library引用可移植类库中的资源文件时找不到 ResourceMap 错误
【发布时间】:2012-05-29 14:01:29
【问题描述】:

我面临的问题如下:

我开发了一个可移植的类库来封装服务连接。在这个类库中有一个包含字符串的 Resources.resw 文件。这些字符串仅由类库的方法调用(例如覆盖 ToString() 方法)。

正如我所说,这是一个可移植的类库。如果我将它作为 dll 引用,或者甚至作为另一个解决方案中的项目引用,它就会被构建并正确编译。然后我在我的应用程序中使用这个库的方法进行调用,比如说

        ClientFacadeConnector connector = new ClientFacadeConnector();
        ICollection<SearchResult> results = null;
        string message = string.Empty;

        if (maxResults != -1) //Search with max Results
        {
            try
            {
                if (!contextQuery.Trim().Equals(string.Empty))
                {

                    results = await connector.GetConnected().SearchAsync(contextQuery, query, maxResults);
                    message = "Search with ContextQuery " + contextQuery + ", Query " + query + ", max results " + maxResults.ToString();
                }
                else
                {

                    results = await connector.GetConnected().SearchAsync(query, maxResults, true);
                    message = "...using normal Query search, Query " + query + ", max results " + maxResults.ToString();
                }
            }
            catch (IQserException ex)
            {
                message = ex.Message;
            }
        }


        if (results != null)
        {
            ICollection<LocalSearchResult> contentResults = new List<LocalSearchResult>();
            foreach (SearchResult s in results)
            {
                var q = s.ToString();
                var contentItem = await connector.GetConnected().GetContentAsync(s.ContentId);
                LocalSearchResult lContent = new LocalSearchResult(contentItem);
                lContent.Score = s.Score;
                lContent.Relevance = s.Relevance;
                lContent.MarkFullText(query);
                contentResults.Add(lContent);
            }

在调用 s.ToString() 方法时,出现错误“找不到资源映射”。

解释这是从哪里来的:

public static class AppResources
{
    private static ResourceLoader resourceLoader;

    static AppResources()
    {
        // Load local file Resources.resw by default
        resourceLoader = new ResourceLoader();            
    }

    public static string GetResources(string key)
    {
        if (string.IsNullOrEmpty(key))
            throw new ArgumentNullException("key");

        return resourceLoader.GetString(key);
    }

}

在被覆盖的 ToString() 方法内部有如下代码:

    public override string ToString()
    {
        StringBuilder buf = new StringBuilder(AppResources.GetResources("InstrSearchResultContent"));

        if (ContentId != -1)
        {
            buf.Append(AppResources.GetResources("StringContent") + " ID:" + ContentId.ToString() + " | ");
        }
        else
        {
            buf.Append(AppResources.GetResources("StringNo") + AppResources.GetResources("StringContent") + "ID" + " | ");
        }
        ...

资源文件名为resources.resw,是ResourceLoader在不调用其他文件时调用的默认resw文件。

奇怪的是,如果我在本地复制客户端应用程序中的资源文件,所有对类库资源文件的调用都会正确引用它,并且一切正常。

这个类库在完成后应该是一个 SDK。我需要单独分发资源文件吗?

这样的问题我在普通的类库和 resx 文件中从未遇到过。 Resw 让我毛骨悚然..

【问题讨论】:

    标签: c# resources portability windows-runtime microsoft-metro


    【解决方案1】:

    看起来你必须在创建ResourceLoader 时指定资源映射的名称,如下所示:

    resourceLoader = new ResourceLoader("Assembly/ResourceFile");
    

    例如,如果您的类库名为“Company.Lib.dll”,而您的资源文件为“Resources.resw”,您将使用:

    resourceLoader = new ResourceLoader("Company.Lib/Resources");
    

    这似乎没有在 MSDN 上完整记录 - 它建议您可以只指定资源文件的名称,但它可能仅适用于 Windows Store 应用程序项目中的资源文件。 It was this page 告诉我,对于库,您还需要指定程序集名称。

    【讨论】:

      【解决方案2】:

      在使用类库开发 UWP 应用时,我遇到了类似的问题。 所以我的库中有一个 /Strings/en-Us/Resource.resw 文件。

      ResourceLoader.GetForCurrentView().GetString("someKey");
      

      给出一个例外

       new ResourceLoader("Company.Lib/Resources").GetString("someKey");
      

      给我一​​个弃用警告但有效。

      我没有给出警告的解决方案是这样的:

      ResourceLoader.GetForViewIndependentUse("AssemblyNamespace/Resources").GetString("someKey");
      

      【讨论】:

        【解决方案3】:

        即使重复 How to load string resources 的所有步骤,我也遇到了类似的问题。 问题是我的 Resources.resw 文件是空的。当我添加一些假字符串时,一切都开始按预期工作。

        【讨论】:

          【解决方案4】:

          @Rory MacLeod 发布的已接受答案可能不再正确。我试过了,VS 警告说 ResourceLoader(String) 已被弃用。以下在我的情况下有效:

          var loader = ResourceLoader.GetForCurrentView();
          string localName = loader.GetString("someKey");
          

          【讨论】:

          • 仍然遇到同样的错误。在此之前我们需要初始化一些东西吗?
          • 我不认为它已被弃用。我现在在 2017 年没有收到任何这样的消息。如果您使用的路径与我相信的字符串默认路径不同,则使用字符串参数
          • 使用 ResourceLoader.GetForCurrentView() 我遇到了同样的错误(“找不到资源映射”)。 new ResourceLoader("Assembly/ResourceFile") 解决方案正在运行,但伴随着弃用警告。
          【解决方案5】:

          我有一个类似的问题,我通过在属性中将 resw 文件的 Build Action 更改为 PRIResource 解决了这个问题。我已将现有 resx 重命名为 resw,但文档没有提到您还必须更改构建操作。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-07-19
            • 2017-04-15
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多