添加EventHandler后,GTK#应用程序崩溃

时间:2017-05-21 16:55:17

标签: c# mono gtk#

我正在使用Mono中的一个小应用程序。我希望在backrgound上有一个图像,每当窗口大小改变时,图像都必须重新绘制。但是当我为ExposeEvent或ConfigureEvent应用程序添加一个方法时。可能是什么? 这是我的代码

using System;
using System.IO;
using Gtk;

public partial class AuthWind: Gtk.Window
{
    FileStream bgstream;
    public AuthWind () : base (Gtk.WindowType.Toplevel)
    {
        bgstream = File.Open ("noise-texture.png", System.IO.FileMode.Open);
        Build ();
        HBox mainCont = new HBox (false, 0);
        ConfigureEvent += DrawBG;
        Gdk.Pixbuf bgbuf = new Gdk.Pixbuf(bgstream, this.Allocation.Width, this.Allocation.Height);
        Gdk.Pixmap bgmap = null;
        Gdk.Pixmap useless = null;
        bgbuf.RenderPixmapAndMask (out bgmap, out useless, 0);
        Style st = new Style ();
        st.SetBgPixmap (StateType.Normal, bgmap);
        this.Style = st;
    }

    void DrawBG(object obj, EventArgs e)
    {
        Gdk.Pixbuf bgbuf = new Gdk.Pixbuf(bgstream, this.Allocation.Width, this.Allocation.Height);
        Gdk.Pixmap bgmap = null;
        Gdk.Pixmap useless = null;
        bgbuf.RenderPixmapAndMask (out bgmap, out useless, 0);
        Style st = new Style ();
        st.SetBgPixmap (StateType.Normal, bgmap);
        this.Style = st;
    }

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

2 个答案:

答案 0 :(得分:0)

你保持bgstream开放,但没有倒回流,也许会崩溃?

另外,请发布崩溃的完整堆栈跟踪。

答案 1 :(得分:0)

经过长时间的搜索,我找到了解决方案。首先必须创建pixbuf一次并保存为类变量。然后在事件处理程序中必须使用ScaleSimple方法。它是Pixbuf类的一种方法。此方法调整pixbuf的大小并创建一个具有所需宽度和高度的新元素。至于事件......必须在你想要使用的窗口中添加Gdk.Event的数量。之后,配置事件必须起作用。