如何在 SharpDevelop 中向 C# 项目添加资源?

How can I add resources to a C# project in SharpDevelop?

当我想要这么select一张图片时,项目资源为空:

如何在那里添加资源?

解决方法,我用的是.resx文件:

  1. 为您的项目创建一个新的资源文件(项目 > 添加 > 新项目 > 杂项 > 空资源文件)。
  2. 右键单击资源编辑器中的空列表(白色space)>添加文件, 选择文件
  3. 添加文件的方式取决于文件类型,添加图像 作为 System.Drawing.Bitmap,二进制文件作为字节数组。

假设我们添加 "Image.png"。要访问这些文件,请使用此代码:

using System.Reflection;
using System.Resources;

ResourceManager resources = new ResourceManager("Namespace.ResourceFile", Assembly.GetExecutingAssembly());
Bitmap bitmap = (Bitmap) resources.GetObject("Image"); //image without extension
myButton.BackgroundImage = bitmap;

Namespace 是应用程序的根名称space,ResourceFile 是 .resx 文件的名称(没有 .resx 扩展名)。如果 .resx 文件位于项目的子文件夹中,请使用 "Namespace.SubfolderName.ResourceFile"。 确保您的图像适合 Control.Size,属性 BackgroungImage 不会调整图像大小。 resx 文件就像一个文件夹,它将包含您的资源。

您的 (@tomyforever) 回答有更直接的解决方案。

  1. 创建一个空的资源文件(即"gfx.resx")并在其中添加文件"Image.png" 这里的默认名称应该是"Image",当然你可以重命名它
  2. 将资源文件(从下拉菜单)的自定义工具 属性 设置为 ResXFileCodeGenerator(有时会自动添加)
  3. 现在它在 UI 项目资源中可见,或者只需在代码中使用 myButton.BackgroundImage = gfx.Image;

如果你在编译时知道程序集名称,你should not use Reflection