C# 在resx文件中返回null的资源

C# 在resx文件中返回null的资源,c#,winforms,embedded-resource,C#,Winforms,Embedded Resource,我试图从一个资源加载一个图像,但是即使该图像在资源文件中,它也会显示为null 声明图像属性 Image selectedImage = null; Image emptyImage = null; ResourceManager rm = new ResourceManager("SchedurlesImages.resx", Assembly.GetExecutingAssembly()); Bitmap selectedImage = (Bitmap)rm.GetObject("se

我试图从一个资源加载一个图像,但是即使该图像在资源文件中,它也会显示为null

声明图像属性

Image selectedImage = null;
Image emptyImage = null;

ResourceManager rm = new ResourceManager("SchedurlesImages.resx", Assembly.GetExecutingAssembly());


Bitmap selectedImage = (Bitmap)rm.GetObject("selected");
Bitmap emptyImage = (Bitmap)rm.GetObject("empty");
在这里,当我单步通过调试器时,它们显示为null L为什么?
您已经声明了两个变量或字段:

Image selectedImage = null;
Image emptyImage = null;
但您正在将资源分配给新的局部变量在变量前面写入类型名称,使其成为新的变量声明:

Bitmap selectedImage = (Bitmap)rm.GetObject("selected");
Bitmap emptyImage = (Bitmap)rm.GetObject("empty");
删除类型名称以使用前面声明的变量或字段

selectedImage = (Image)rm.GetObject("selected");
emptyImage = (Image)rm.GetObject("empty");
如果图像位于当前项目的资源中,则可以使用访问它们

selectedImage = Properties.Resources.selected;
emptyImage = Properties.Resources.empty;

Visual Studio使用硬编码和强类型资源创建代码隐藏文件。

这不是C,不是。@alk我选择嵌入资源它就是它,因此标记是依赖的。您将错误的参数传递给ResourceManager构造函数。不要这样做,请使用项目>属性>资源。按F1键了解更多信息。它在同一个项目中,但该项目在解决方案中,这是否意味着我不能使用底部方法,因为当尝试访问它时,它仍然不工作项目始终在解决方案中。如果bottom方法进行编译,它就可以工作。您的代码无法工作,因为您有两组不同的2个变量。你有4个变量。