【发布时间】:2012-12-06 18:17:42
【问题描述】:
所以我有一个 Windows 窗体 dll。有一个 MainForm 类,它创建一个 ComponentResourceManager 并访问存储在该表单的 resx 中的图像资源。如果我直接实例化该类,一切都会很愉快。
如果我将一个新类添加到同一个命名空间(实际上我只是将它添加到与 MainForm 类相同的 .cs 文件中)并且我尝试访问这些图像资源的方式与我收到关于该资源不存在的错误的方式相同对于给定的文化。检查当前的线程文化(和 UI 文化)它们是相同的。
我需要跳过一些额外的环节才能从另一个类访问表单的资源吗?
示例代码
namespace myNamespace
{
public class extraClass
{
public extraClass()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(myForm));
System.Drawing.Image img = ((System.Drawing.Image)(resources.GetObject("StatusButton.Image")));
MessageBox.Show(img.ToString());
}
}
public class myForm : Form
{
public myForm()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(myForm));
System.Drawing.Image img = ((System.Drawing.Image)(resources.GetObject("StatusButton.Image")));
MessageBox.Show(img.ToString());
InitializeComponent();
}
}
}
在此示例中,实例化 myForm 会很高兴,但 extraClass 不会,并且失败并出现关于无法在给定文化中找到资源的错误 & 我是否确保资源已嵌入(它是)。
【问题讨论】:
-
越来越疯狂。所以我很懒惰,将我的新类 extraClass 添加到与 myForm 相同的 cs 文件中。事实证明,存在 extraClass,即使我将其剥离为具有空构造函数的空类,也会导致主 myForm 在无法找到资源的地方出现相同的错误。我将尝试将 extraClass 移动或移动到另一个文件。也许是因为我使用的是 Python 中的这个 dll,而 python clr 桥做了一些疯狂的事情?
标签: c# winforms embedded-resource resx