使用ImageList时候发生运行错误提示资源问题的解决方法

类别:.NET开发 点击:0 评论:0 推荐:

今天在给一个程序加入ImageList的时候碰到了这样的运行错误:

未能在给定的程序集中找到任何适合于指定的区域性(或非特定区域性)的资源。请确保已将“frmMain.resources”正确嵌入或链接到程序集“XXX”。
baseName: yyy  locationInfo: xxx.yyy  resource file name: yyy.resources  assembly: XXX, Version=1.0.1698.16471, Culture=neutral, PublicKeyToken=null

其实之前在给这个程序的主窗口自定义icon的时候也碰到过同样的问题,只是当时没有注意。
查找资料,在微软的支持站点发现如下文章:

http://support.microsoft.com/default.aspx?scid=kb;en-us;Q318603

PRB: Error When You Access .resources File of the Form at Run Time

Article ID : 318603
Last Review : August 7, 2003
Revision : 2.0
This article was previously published under Q318603

SYMPTOMS

When you are running your Visual Basic .NET, Visual C# .NET, or Visual J# .NET Windows application, you may receive the following error message or a similar one when you load a form:
An unhandled exception of type 'System.Resources.MissingManifestResourceException' occurred in mscorlib.dll

Additional information: Could not find any resources appropriate for the specified culture (or the neutral culture) in the given assembly. Make sure "Form1.resources" was correctly embedded or linked into assembly "myApplication".

baseName: Form1 locationInfo: WindowsApplication4.Form1 resource file name: Form1.resources assembly: myApplication, Version=1.0.781.33026, Culture=neutral, PublicKeyToken=null
If you click Break in the error dialog box, and if you are running the code in the integrated development environment (IDE), you discover that a line of code inside the InitializeComponent statement causes this problem. For example, if you create the sample from the "More Information" section, this error occurs in conjunction with an ImageList control. Although the error message may refer to a different control, notice that resources.GetObject appears on that line of code in the error message:
this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));

CAUSE

This problem occurs because the Form class is not the first class in the code module of the form.

NOTE: This article documents one specific cause of this problem. There may be other situations that can lead to similar results.

RESOLUTION

To resolve this problem, move all of the other class definitions so that they appear after the form's class definition.

STATUS

This behavior is by design.

MORE INFORMATION

A form code module can contain multiple classes. When the project is built, the build system must decide which class it should use to create the .resources file. The design of the project build system specifies that the first class in the code module is the class on which the .resources file is based. The .resources file is named according to that class, not the name of the form. In most cases, these two names are the same. However, if these names differ, you receive the error message that is listed in the "Symptoms" section.

When you load the form at run time, the InitializeComponent method may need to get objects from the .resources file. InitializeComponent searches for the Form_Name.resources file in the assembly. Because the initial Form_Name.resources file was never created or linked to the assembly, the attempt to retrieve the .resources file fails.

Steps to Reproduce the Behavior

1. Create a Windows Application project in Visual Basic .NET, Visual C# .NET, or Visual J# .NET. Form1 is created by default.
2. Add an ImageList control to Form1.
3. Add an icon file to the ImageList control.
4. In the code module of Form1, add the following code before the Form1 definition: Visual Basic .NET:
Public Class someClass
End Class
					
Visual C# .NET and Visual J# .NET:
public class someClass
{
}
					
5. Build the project. Notice that the project builds successfully.
6. Run the project. Notice that you receive the error message that is listed in the "Symptoms" section.

APPLIES TO
Microsoft Visual Basic .NET 2002 Standard Edition
Microsoft Visual C# .NET 2002 Standard Edition
Microsoft Visual J# .NET 2003 Standard Edition
Microsoft Visual J# .NET 2003 Standard Edition
Microsoft Visual C# .NET 2003 Standard Edition
Microsoft Visual Basic .NET 2003 Standard Edition


把主窗体的类提到程序最前面,问题解决。^_^

本文地址:http://com.8s8s.com/it/it43263.htm