GTK#图像按钮在运行时不显示图像

时间:2015-06-05 10:39:51

标签: c# xamarin mono gtk gtk#

我试图在GTK中使用图像按钮#(Xamarin Studio)。我将图像设置为按钮,在UI Builder中,图像即将出现。

enter image description here

但是当我运行程序时,按钮中没有图像

enter image description here

我在不同版本的IDE和不同平台(Mac和Windows)上尝试了这个

请帮忙

更新: MainWindow.cs

using System;
using Gtk;

public partial class MainWindow: Gtk.Window
{
    public MainWindow () : base (Gtk.WindowType.Toplevel)
    {
        Build ();
    }

    protected void OnDeleteEvent (object sender, DeleteEventArgs a)
    {
        Application.Quit ();
        a.RetVal = true;
    }
}

gtk-gui 文件夹中,我可以找到这些文件

generated.cs

// This file has been generated by the GUI designer. Do not modify.
namespace Stetic
{
    internal class Gui
    {
        private static bool initialized;

        internal static void Initialize (Gtk.Widget iconRenderer)
        {
            if ((Stetic.Gui.initialized == false)) {
                Stetic.Gui.initialized = true;
            }
        }
    }

    internal class IconLoader
    {
        public static Gdk.Pixbuf LoadIcon (Gtk.Widget widget, string name, Gtk.IconSize size)
        {
            Gdk.Pixbuf res = widget.RenderIcon (name, size, null);
            if ((res != null)) {
                return res;
            } else {
                int sz;
                int sy;
                global::Gtk.Icon.SizeLookup (size, out  sz, out  sy);
                try {
                    return Gtk.IconTheme.Default.LoadIcon (name, sz, 0);
                } catch (System.Exception) {
                    if ((name != "gtk-missing-image")) {
                        return Stetic.IconLoader.LoadIcon (widget, "gtk-missing-image", size);
                    } else {
                        Gdk.Pixmap pmap = new Gdk.Pixmap (Gdk.Screen.Default.RootWindow, sz, sz);
                        Gdk.GC gc = new Gdk.GC (pmap);
                        gc.RgbFgColor = new Gdk.Color (255, 255, 255);
                        pmap.DrawRectangle (gc, true, 0, 0, sz, sz);
                        gc.RgbFgColor = new Gdk.Color (0, 0, 0);
                        pmap.DrawRectangle (gc, false, 0, 0, (sz - 1), (sz - 1));
                        gc.SetLineAttributes (3, Gdk.LineStyle.Solid, Gdk.CapStyle.Round, Gdk.JoinStyle.Round);
                        gc.RgbFgColor = new Gdk.Color (255, 0, 0);
                        pmap.DrawLine (gc, (sz / 4), (sz / 4), ((sz - 1) - (sz / 4)), ((sz - 1) - (sz / 4)));
                        pmap.DrawLine (gc, ((sz - 1) - (sz / 4)), (sz / 4), (sz / 4), ((sz - 1) - (sz / 4)));
                        return Gdk.Pixbuf.FromDrawable (pmap, pmap.Colormap, 0, 0, 0, 0, sz, sz);
                    }
                }
            }
        }
    }

    internal class ActionGroups
    {
        public static Gtk.ActionGroup GetActionGroup (System.Type type)
        {
            return Stetic.ActionGroups.GetActionGroup (type.FullName);
        }

        public static Gtk.ActionGroup GetActionGroup (string name)
        {
            return null;
        }
    }
}

Mainwindow.cs

// This file has been generated by the GUI designer. Do not modify.

public partial class MainWindow
{
    private global::Gtk.Fixed fixed1;

    private global::Gtk.Button button1;

    protected virtual void Build ()
    {
        global::Stetic.Gui.Initialize (this);
        // Widget MainWindow
        this.Name = "MainWindow";
        this.Title = global::Mono.Unix.Catalog.GetString ("MainWindow");
        this.WindowPosition = ((global::Gtk.WindowPosition)(4));
        // Container child MainWindow.Gtk.Container+ContainerChild
        this.fixed1 = new global::Gtk.Fixed ();
        this.fixed1.Name = "fixed1";
        this.fixed1.HasWindow = false;
        // Container child fixed1.Gtk.Fixed+FixedChild
        this.button1 = new global::Gtk.Button ();
        this.button1.CanFocus = true;
        this.button1.Name = "button1";
        this.button1.UseUnderline = true;
        this.button1.Label = global::Mono.Unix.Catalog.GetString ("GtkButton");
        global::Gtk.Image w1 = new global::Gtk.Image ();
        w1.Pixbuf = global::Stetic.IconLoader.LoadIcon (this, "gtk-delete", global::Gtk.IconSize.Menu);
        this.button1.Image = w1;
        this.fixed1.Add (this.button1);
        global::Gtk.Fixed.FixedChild w2 = ((global::Gtk.Fixed.FixedChild)(this.fixed1 [this.button1]));
        w2.X = 153;
        w2.Y = 137;
        this.Add (this.fixed1);
        if ((this.Child != null)) {
            this.Child.ShowAll ();
        }
        this.DefaultWidth = 400;
        this.DefaultHeight = 300;
        this.Show ();
        this.DeleteEvent += new global::Gtk.DeleteEventHandler (this.OnDeleteEvent);
    }
}

2 个答案:

答案 0 :(得分:7)

据我所知,代码没有任何问题。这与Gtk的主题有关。

根据主题的设置方式,您可能会看到图标。顺便说一句,Xamarin使用的主题与默认使用的主题不同,因此从Xamarin Studio(F5)或命令行运行应用程序可能会产生不同的结果。

如果我记得很清楚,在Windows下,有一个默认的Gtk主题和一个Windows主题。有一个切换主题的工具,但不幸的是,我可以得到它。

编辑:

以下是如何让主题运行:

这是一个可以让您选择主题的小软件:http://sourceforge.net/projects/tumagcc/files/gtk2_prefs.exe/download

你还必须在网络的某个地方下载一个Gtk主题(我试过的第一个在Windows上变得丑陋且无法使用,所以你可能需要在这里进行一些实验)。将主题(通常是tar.gz文件)解压缩到共享/主题下的Gtk安装。提取主题后,确保目录结构如下:

{Gtk}/share/themes/MyFantasticTheme/Gtk-2.0

然后以管理员身份启动小软件,在列表中选择您的主题,然后单击“确定”。现在从Xamarin Studio外部运行您的应用程序。

编辑#2:

经过一番摆弄后,我发现了一种在按钮中显示图标和标签的方法:

Application.Init ();
Gtk.Settings.Default.ThemeName = "Theme/gtk-2.0/gtkrc";
Gtk.Rc.Parse ("./Theme/gtk-2.0/gtkrc");

我的主题由Theme/gtk-2.0/gtkrc内的一行组成(有关gtkrc文件的文档很容易找到,因为它不是Gtk#specific):

gtk-button-images = 1

我认为发生的是在应用程序启动时以某种方式设置默认主题,并且无法覆盖此默认主题。为什么会这样?也许是因为默认主题使用特定的Gtk引擎运行,不允许按钮中的图标。通过覆盖默认主题,我相信您将Gtk切换到默认引擎。

答案 1 :(得分:3)

初始化Gtk后,您可以通过以下方式获得相同的效果。

//Application.Init(); - insert after this line
Gtk.Settings.Default.SetLongProperty ("gtk-button-images", 1, "");
相关问题