找不到适合指定文化或中性文化的资源
我创建了一个程序集,后来重命名了它。
然后我在调用时开始收到运行时错误:
toolsMenuName = resourceManager.GetString(resourceName);
resourceName
变量在运行时是“enTools” 。
找不到适合指定文化或中性文化的资源。 确保在编译时将“Jfc.TFSAddIn.CommandBar.resources”正确嵌入或链接到程序集“Jfc.TFSAddIn”中,或者所有所需的附属程序集都是可加载和完全签名的。
代码:
string resourceName; ResourceManager resourceManager = new ResourceManager("Jfc.TFSAddIn.CommandBar", Assembly.GetExecutingAssembly()); CultureInfo cultureInfo = new CultureInfo(_applicationObject.LocaleID); if(cultureInfo.TwoLetterISOLanguageName == "zh") { CultureInfo parentCultureInfo = cultureInfo.Parent; resourceName = String.Concat(parentCultureInfo.Name, "Tools"); } else { resourceName = String.Concat(cultureInfo.TwoLetterISOLanguageName, "Tools"); } toolsMenuName = resourceManager.GetString(resourceName); // EXCEPTION IS HERE
我可以看到项目中包含的CommandBar.resx文件,我可以打开它并在那里看到“enTools”字符串。 似乎任何资源都不包含在程序集或资源中,但.NET无法解析名称。
我认为更简单的解决方案是为每种语言创建单独的资源文件。
就此情况而言,检查包含资源的程序集是否将默认名称空间设置为相同的文本(Project-> Properties-> Default namespace;在VS中)
检查resx文件是否具有将BuildAction属性设置为“Embedded resource”的属性
听起来与我们遇到的问题类似。 资源文件的设计器中的命名空间不正确。 我通过在resx文件上手动重新运行自定义工具来修复它。
右键单击your.resx,然后单击“运行自定义工具”。
我相信你已经得到了答案,但以防万一:
-
您可以通过调用查看ManifestResourceName
System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceNames()
检查GetString()调用中的Manifest名称和名称是否相同。
-
另外,请确保在designer.resx文件中具有正确的命名空间:
namespace Jfc.TFSAddIn { ... global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager( "Jfc.TFSAddIn.CommandBar", typeof(CommandBar).Assembly); ... }
-
打开resx文件属性:“Build Action”应为“Embedded Resource”
对我来说,问题的根源是命名以数字开头的rex文件:
20160216_tranlation.resx
在调用GetGlobalResourceObject
时,我必须在resx文件名之前添加下划线_
:
public static string getResource(string key) { return HttpContext.GetGlobalResourceObject("_20160216_tranlation", key).ToString(); }
这个答案为我解决了问题! GetGlobalResourceObject
我更正了ResourceManager静态属性中的设计器文件(Resources.Designer.cs)中的命名空间,它对我有用。
请参阅以下代码:
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] internal static global::System.Resources.ResourceManager ResourceManager { get { if (object.ReferenceEquals(resourceMan, null)) { global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("XYZAssembly.Properties.Resources", typeof(Resources).Assembly); resourceMan = temp; } return resourceMan; } }
我在Form.cs中添加了一个临时类,同时(测试||调试)导致抛出此exception。 Form.resx文件(名称||资源ID)已修改为临时类名而不是Form类名。 这给我带来了问题。 通过在项目中为我的临时类创建一个单独的文件,我(更正了||缓解了这一点)。
一个解决方案是将resx文件的属性从内容更改为嵌入式资源并构建它。这次你可以得到
我在Xamarin.Forms中遇到过这个问题,当我尝试重命名项目时,资源无法再加载相同的错误文本。
要解决此问题,我必须通过文本编辑器修改.csproj,并更改嵌入资源的逻辑名称。
ResXFileCodeGenerator TextResources.Designer.cs YourNewNamespaceName.TextResources.resources Designer
在重建它时还要注意自动生成的类,其中所述的命名空间可能会发生变化。
希望它可以帮助那些陷入同样情况的人。
我在Windows窗体应用程序中添加了一个类ABOVE部分表单类时出现此错误。
当我将课程移到部分表格课程之后,它就消失了。