简体   繁体   English

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

[英]GTK# application crashes after adding a EventHandler

I'm working on a little application in Mono. 我正在Mono中开发一个小应用程序。 I'd like to have an image on a backrgound and everytime when window size is changing image has to be redrawn. 我想在backrgound上有一个图像,并且每次窗口大小改变时都必须重新绘制图像。 But when I add a method for ExposeEvent or ConfigureEvent application falls. 但是当我为ExposeEvent或ConfigureEvent应用程序添加方法时会掉线。 What might it be? 可能是什么? Here is my code 这是我的代码

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;
    }
}

You are keeping the bgstream open, but not rewinding the stream, maybe that crashes things? 您将bgstream保持打开状态,但不倒带该流,也许这会使事情崩溃?

Also, please post the full stack trace for the crash. 另外,请发布崩溃的完整堆栈跟踪。

After long search I have found a solution. 经过长时间的搜索,我找到了解决方案。 Firstly pixbuf have to be created once and saved as class variable. 首先,pixbuf必须创建一次并保存为类变量。 Then in the event handler have to be used ScaleSimple method. 然后在事件处理程序中必须使用ScaleSimple方法。 It a method of a Pixbuf class. 它是Pixbuf类的方法。 This method resize a pixbuf and create new one with width and height you need. 此方法调整pixbuf的大小并创建具有所需宽度和高度的pixbuf。 As for event... It has to be added number of Gdk.Event in the window you want to work with. 至于事件...它必须在您要使用的窗口中添加Gdk.Event的数量。 After that configure event has to work. 之后,配置事件必须起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM