引用可移植类库中的资源文件时找不到ResourceMap错误

Ach*_* P. 11 c# resources portability microsoft-metro windows-runtime

我面临的问题如下:

我开发了一个可移植的类库来封装服务连接.在这个类库中有一个包含字符串的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);
            }
Run Code Online (Sandbox Code Playgroud)

在我调用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);
    }

}
Run Code Online (Sandbox Code Playgroud)

在重写的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" + " | ");
        }
        ...
Run Code Online (Sandbox Code Playgroud)

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

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

完成后,该类库应该是一个SDK.我是否需要单独分发资源文件?

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

Ror*_*eod 13

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

resourceLoader = new ResourceLoader("Assembly/ResourceFile");
Run Code Online (Sandbox Code Playgroud)

例如,如果您的类库名为"Company.Lib.dll",并且您的资源文件是"Resources.resw",那么您将使用:

resourceLoader = new ResourceLoader("Company.Lib/Resources");
Run Code Online (Sandbox Code Playgroud)

这似乎没有在MSDN上完全记录 - 它建议您只需指定资源文件的名称,但它可能只适用于Windows应用商店应用程序项目中的资源文件.正是这个页面向我展示了,对于库,您还需要指定程序集名称.


Eug*_*oft 8

即使重复了如何加载字符串资源的所有步骤,我也遇到了类似的问题.问题是我的Resources.resw文件是空的.当我添加一些假字符串时,所有开始按预期工作.


joy*_*ym8 2

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

var loader = ResourceLoader.GetForCurrentView();
string localName = loader.GetString("someKey");
Run Code Online (Sandbox Code Playgroud)

  • 仍然遇到同样的错误。在此之前我们需要初始化一些东西吗? (2认同)