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

GTK# application crashes after adding a EventHandler

我正在用 Mono 开发一个小应用程序。我想在背景上放一张图片,每次 window 尺寸改变时都必须重新绘制图片。但是,当我为 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;
    }
}

您保持 bgstream 打开,但没有倒回流,这可能会导致崩溃?

此外,请 post 崩溃的完整堆栈跟踪。

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