2012-05-29 125 views
9

我現在面臨的問題有如下:ResourceMap未找到錯誤

我已經開發出一種便攜式類庫來封裝服務連接。在這個類庫裏面有一個包含字符串的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給我的毛骨悚然..

回答

2

接受的答案張貼@Rory MacLeod可能不再是真實的。我試過並且VS警告ResourceLoader(String)已被棄用。以下在我的情況下工作:

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

仍然收到相同的錯誤。我們需要在此之前初始化一些東西嗎? – hellodear

+0

我不認爲這是棄用。我現在沒有在2017年收到類似的消息。如果使用的字符串參數不是字符串,那麼使用String參數默認路徑我相信 – batmaci

+0

With ** ResourceLoader.GetForCurrentView()**我得到相同的錯誤(「Resource Map not found」)。 **新的ResourceLoader(「Assembly/ResourceFile」)**解決方案正在運行,但伴隨有棄用警告。 – jannikb

4

我有一個類似的問題,我通過將屬性中的resw文件的生成操作更改爲PRIResource來解決。我已經將現有的resx重命名爲resw,但文檔沒有提及您還必須更改構建操作。

12

它看起來像你必須在創建ResourceLoader,這樣的指定資源地圖的名稱:

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

例如,如果你的類庫被稱爲「Company.Lib.dll」和你的資源文件是「Resources.resw」,你可以使用:

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

這似乎並不被MSDN上完整記錄 - 這意味着你可以指定你的資源文件的名稱,但它可能只適用於窗口中的資源文件s商店應用程序項目。 It was this page,這表明,對於庫,您還需要指定程序集名稱。

+0

這幫助我出去! +1 – borrrden

+0

幫助了我很多,謝謝+1 – katho2404

7

我也有類似的問題,即使重複從How to load string resources所有步驟。 問題是我的Resources.resw文件是空的。當我添加一些假字符串時,它們都按預期開始工作。

0

我遇到類似的問題,當開發一個類庫的UWP應用程序。 所以我在我的庫中有一個/Strings/en-Us/Resource.resw文件。

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

給出了一個例外

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

給我不贊成警告,但工程。

我的解決方案不發出警告的是:

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