0

语境

我是 Linux 下 Vala 开发的新手(尽管我有几年的 C# 经验),我决定重新创建我的一个 C# 程序,但是,我需要在 UI 中使用图像。

我的问题

如何嵌入资源文件(例如图像)以供以后在 UI 中使用?以后如何访问它们?以及如何将它们放入按钮中?

我正在使用什么

我正在使用 Linux (Mint) 和 Anjuta 开发 IDE,并集成了 Glade UI 设计器。Vala 项目以 GTK+ 3.0 项目为目标。

我试过的

我已经尝试为图像添加一个新的特定目标,将它们添加到项目中......但我似乎没有成功。我已经看到了 Linux 提供的默认图像,这些图像工作得很好,但我需要添加我自己的。

提前致谢!

4

1 回答 1

0

The normal approach would be to install your images to PREFIX/share/pixmaps/YOUR_APP. For example Gnumeric installs some .png files in /usr/share/pixmaps/gnumeric/.

You can use GResource to embed binary files (like images) into your executable if you really want to. The glib-compile-resources command can be added to your build system (see also this question).

You can also use icons from the users icon theme.

You didn't write what component you want to use to display your images, so I'll assume Gtk.Image here.

Gtk.Image has several constructors for the purpose of loading the image:

  • from_icon_name loads the image from the current icon theme (which is useful to support user themes).
  • from_resource loads the image from an embedded GResource.
  • from_file loads the image from a file.

See the main documentation of Gtk.Image for more methods. Some other widgets have similar methods to load images (for example toolbar buttons).

You should not use from_stock any more (There was a stock system in Gtk+ that is being replaced by freedesktop.org icon schemas).

于 2016-06-05T18:00:34.423 回答