我面临的问题如下:
我开发了一个可移植的类库来封装服务连接。在这个类库中有一个包含字符串的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给了我小兵......
答案 0 :(得分:12)
看起来您必须在创建ResourceLoader
时指定资源地图的名称,如下所示:
resourceLoader = new ResourceLoader("Assembly/ResourceFile");
例如,如果您的类库名为“Company.Lib.dll”,而您的资源文件是“Resources.resw”,则可以使用:
resourceLoader = new ResourceLoader("Company.Lib/Resources");
这似乎没有在MSDN上完全记录 - 它建议您只能指定资源文件的名称,但它可能只适用于Windows应用商店应用程序项目中的资源文件。 It was this page向我展示了,对于库,您还需要指定程序集名称。
答案 1 :(得分:8)
即使重复How to load string resources的所有步骤,我也遇到了类似的问题。 问题是我的Resources.resw文件是空的。当我添加一些假字符串时,它们都按预期开始工作。
答案 2 :(得分:4)
我有一个类似的问题,我通过将resw文件的Build Action更改为属性中的PRIResource来解决。我已经重命名了一个现有的resx来重新启动,但是文档没有提到你还必须改变构建操作。
答案 3 :(得分:2)
@Rory MacLeod发表的已接受的答案可能不再适用。我试过,VS警告ResourceLoader(String)
已被弃用。以下是我的案例:
var loader = ResourceLoader.GetForCurrentView();
string localName = loader.GetString("someKey");
答案 4 :(得分:1)
在开发带有类库的UWP应用程序时遇到了类似的问题。 所以我的库里有一个/Strings/en-Us/Resource.resw文件。
props.onClick
给出例外
ResourceLoader.GetForCurrentView().GetString("someKey");
给我一个弃用警告,但有效。
我的解决方案没有发出警告:
new ResourceLoader("Company.Lib/Resources").GetString("someKey");