找不到适合指定区域性的任何资源(资源文件中的 C# WinForm 图像)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1923953/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Could not find any resources appropriate for the specified culture (C# WinForm Images in Resource File)
提问by NealWalters
I added three images to a file called Resource1.resx. I also added one string just for test purposes. I'm getting this error on either the GetString or the GetObject(image-name).
我将三个图像添加到名为 Resource1.resx 的文件中。我还添加了一个字符串仅用于测试目的。我在 GetString 或 GetObject(image-name) 上收到此错误。
{"Could not find any resources appropriate for the specified culture or the neutral culture. Make sure \"Resource1.resources\" was correctly embedded or linked into assembly \"TFBIC.RCT.Monitor\" at compile time, or that all the satellite assemblies required are loadable and fully signed."}
{"找不到适合指定文化或中性文化的任何资源。请确保在编译时将 \"Resource1.resources\" 正确嵌入或链接到程序集 \"TFBIC.RCT.Monitor\",或者所有所需的附属程序集可加载且已完全签名。"}
// get initial images
ResourceManager rm = new ResourceManager(
"Resource1",System.Reflection.Assembly.GetExecutingAssembly());
CultureInfo ci = Thread.CurrentThread.CurrentCulture;
string strTest = rm.GetString("String1"); // just testing
Bitmap bmCircleGreen = (Bitmap)rm.GetObject("circleGreen");
Bitmap bmCircleYellow = (Bitmap)rm.GetObject("circleYellow");
Bitmap bmCircleRed = (Bitmap)rm.GetObject("circleRed");
My form is the first class in my project (I've already seen that error).
I assigned a strong-key to my project to no avail.
Not sure what else to try.
我的表单是我项目中的第一类(我已经看到了那个错误)。
我为我的项目分配了一个强键,但无济于事。
不知道还有什么可以尝试的。
采纳答案by Mikael Svenson
Have you remembered to include the default namespace/folder when you reference the resource?
您是否记得在引用资源时包含默认命名空间/文件夹?
ResourceManager rm = new ResourceManager("DefaultNamespace.Folder.ResourceName");
If you are unsure of the correct name, load the assembly in Reflector and browse down to see what it is.
如果您不确定正确的名称,请在 Reflector 中加载程序集并向下浏览以查看它是什么。
回答by Hans Passant
The 1st argument is wrong. But, there is already a ResourceManager created for you. You can see its code: in the Solution Explorer window open the Properties node, open the Resources.resx node and double-click the Resources.Designer.cs file.
第一个论点是错误的。但是,已经为您创建了一个 ResourceManager。您可以看到它的代码:在解决方案资源管理器窗口中打开属性节点,打开 Resources.resx 节点并双击 Resources.Designer.cs 文件。
You'll get its instance with Properties.Resources.ResourceManager
. If you added the bitmaps with Project + Properties, Resources tab (strongly recommended), you can just refer to the property by the name you gave it. Like Properties.Resources.circleGreen
. Do beware that you get a newimage object each time you use the property, you may need to copy it to a variable if you use it more than once.
你会得到它的实例Properties.Resources.ResourceManager
。如果您使用“项目 + 属性”、“资源”选项卡(强烈推荐)添加位图,则可以仅通过您提供的名称来引用该属性。喜欢Properties.Resources.circleGreen
。请注意,每次使用该属性时都会获得一个新的图像对象,如果多次使用它,则可能需要将其复制到变量中。
回答by Shalini
I also had the same problem. adding "DefaultNameSpace." ("BusinessLogic" is the default namespace in my case) before resourcefileName in following line resolved my problem
我也有同样的问题。添加“默认名称空间”。(“BusinessLogic”是我的情况下的默认命名空间)在以下行中的 resourcefileName 解决了我的问题之前
public static ResourceManager rm = new ResourceManager(string.Concat("BusinessLogic." , Constants.Common.ResourceFileName), Assembly.GetExecutingAssembly());
回答by Tobias Knauss
I had the same problem today on a VS-designer-built winform. I had added an image to an item of a menuStrip:
我今天在 VS 设计师构建的 winform 上遇到了同样的问题。我已将图像添加到 menuStrip 的项目中:
this->menuitemFileSettings->Image = (cli::safe_cast<System::Drawing::Image^>
(resources->GetObject(L"menuitemFileSettings.Image")));
Since then the same error came up. Testing with images on other controls was the same.
从那以后出现了同样的错误。在其他控件上使用图像进行测试是相同的。
- Based on MS KB, the Form class must be the first in the code. This was was fine in my code.
- The resouces file also has the same name (frmMain.resx) as the winforms class (frmMain).
- Checking the created exe using ILSpy, I even found the image resource in the file. Something I retrospectively don't really understand.
- I am not using any localization.
- I created a new winform, copied all relevant parts there, same failure.
- 基于MS KB,Form 类必须是代码中的第一个。这在我的代码中很好。
- resouces 文件也具有与 winforms 类 (frmMain) 相同的名称 (frmMain.resx)。
- 使用ILSpy检查创建的exe,我什至在文件中找到了图像资源。一些我回想起来并不真正理解的东西。
- 我没有使用任何本地化。
- 我创建了一个新的 winform,在那里复制了所有相关部分,同样的失败。
Somehow I finally stumbled over the "Managed Resources"entry in the project settings. The setting "resource file name"has the (default) value $(IntDir)\$(RootNamespace).$(InputName).resources
... which is correct, but when I realized that the path contains the name of the namespace, I checked the namespace and found, that I had changed it (from NS_Winform to NS_assemblies) for easier import of the assemblies.
不知何故,我终于偶然发现了项目设置中的“托管资源”条目。设置“资源文件名”具有(默认)值$(IntDir)\$(RootNamespace).$(InputName).resources
......这是正确的,但是当我意识到路径包含名称空间的名称时,我检查了名称空间并发现我已经将其更改(从 NS_Winform 到NS_assemblies) 以便更轻松地导入程序集。
Changed it back, working fine now. :-)
But I still don't understand, how the resources content
1) was included into the exe despite the wrong namespace, and
2) not found then although included.
改回来了,现在工作正常。:-)
但我仍然不明白,资源内容
1) 是如何被包含到 exe 中的,尽管命名空间有误,以及
2) 虽然包含了但未找到。