Could not find any resources appropriate for the s

2019-02-11 22:04发布

I have created an assembly and later renamed it.

Then I started getting runtime errors when calling:

toolsMenuName = resourceManager.GetString(resourceName);

The resourceName variable is "enTools" at runtime.

Could not find any resources appropriate for the specified culture or the neutral culture. Make sure "Jfc.TFSAddIn.CommandBar.resources" was correctly embedded or linked into assembly "Jfc.TFSAddIn" at compile time, or that all the satellite assemblies required are loadable and fully signed.

The code:

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

I can see the file CommandBar.resx included in the project, I can open it and can see the "enTools" string there. It seems that either resources are not included into assembly or resource are included but .NET cannot resolve the name.

10条回答
小情绪 Triste *
2楼-- · 2019-02-11 22:06

Got this error when I added a class ABOVE the partial form class in my Windows forms app.

It went away when I moved the class BELOW the partial form class.

查看更多
欢心
3楼-- · 2019-02-11 22:07

I have encountered this issue in Xamarin.Forms, when I tried to the rename the project, the resources could not be loaded anymore with the same stated error text.

To fix the problem I had to modify the .csproj by a text editor, and change the logical name of the embedded resource.

<EmbeddedResource Include="Localization\TextResources.resx">
      <Generator>ResXFileCodeGenerator</Generator>
      <LastGenOutput>TextResources.Designer.cs</LastGenOutput>
      <LogicalName>YourNewNamespaceName.TextResources.resources</LogicalName>
      <SubType>Designer</SubType>
</EmbeddedResource>

Also watch out for the autogenerated class when you rebuild it, the namespace stated in there might change.

Hope it helps someone that went into the same situation.

查看更多
萌系小妹纸
4楼-- · 2019-02-11 22:10

This answer solved the problem for me! GetGlobalResourceObject

查看更多
Ridiculous、
5楼-- · 2019-02-11 22:21

I think simpler solution would be to create separate resources file for each language.

As far as this case is concerned check if the assembly containing resources has the default namespace set to the same text (Project->Properties->Default namespace; in VS)

Check as well if the resx file has a property BuildAction set to "Embedded resource"

查看更多
太酷不给撩
6楼-- · 2019-02-11 22:22

For me, the source of the problem was naming the rex files starting with a number:

20160216_tranlation.resx

I had to add an underscore _ before the resx file name when calling GetGlobalResourceObject:

public static string getResource(string key)
{
    return HttpContext.GetGlobalResourceObject("_20160216_tranlation", key).ToString();
}
查看更多
在下西门庆
7楼-- · 2019-02-11 22:23

I corrected the namespace in designer file (Resources.Designer.cs) in ResourceManager static property & it worked for me.

See the code below:

[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;
        }
    }
查看更多
登录 后发表回答