1

我对 statuicon 有一个非常奇怪的问题。

我正在做一个简单的项目来保存和显示表中的一些数据,我有一个主窗口(MainWindow),用户在其中插入数据,然后还有另一个窗口显示数据(SumList)。还有一个状态图标,我通过子类化 Gtk.StatusIcon 创建了一个状态图标。问题是,当我启动应用程序并显示应该显示数据的窗口(一切正常)然后关闭窗口(无论如何)时,statusIcon 从面板中消失。

我还发现这是因为类 SumList 的构造函数的长度。如果我从那里删除一些行(随机顺序),状态图标工作正常。

我该如何解决这种奇怪的行为?

编辑#1 我尽量不将 StatusIcon 子类化,而是将其声明为 MainClass 的静态成员,现在它可以正常工作,很奇怪。无论如何,问题是如果 statusIcon 未声明为静态的,为什么它不起作用?

MainClass (StatusIcon)

class MainClass : StatusIcon
{
    MainWindow window;

    private MainClass()
    {
        window = new MainWindow();
        window.Show();

        Stock = Gtk.Stock.Home;

        PopupMenu += rightClick;
        Activate += leftClick;
    }

    private void rightClick (object sender, Gtk.PopupMenuArgs evt){

        window.Hide();
    }

    private void leftClick (object sender, EventArgs e){
        window.Show();

    }


    public static void Main (string[] args)
    {
        Application.Init ();
        new MainClass();

        Application.Run ();
    }
}

SumList 类

public partial class SumList : Gtk.Window
{       
    public SumList () : base(Gtk.WindowType.Toplevel)
    {
        this.Build ();      
        // create the "title" column ------------ //
        TreeViewColumn title = new TreeViewColumn();
        CellRendererText titleR = new CellRendererText();
        title.PackStart(titleR, true);          
        title.AddAttribute(titleR, "text", 0);

        // create the "detial" column ----------- //
        TreeViewColumn detail = new TreeViewColumn();
        CellRendererText detailR = new CellRendererText();
        detail.PackStart(detailR, true);
        detail.AddAttribute(detailR, "text", 1);

        // create the "price" column ------------ //
        TreeViewColumn price = new TreeViewColumn();
        CellRendererText priceR = new CellRendererText();
        price.PackStart(priceR, true);
        price.AddAttribute(priceR, "text", 2);

        // create the "date" column ------------- //
        TreeViewColumn date = new TreeViewColumn();
        CellRendererText dateR = new CellRendererText();
        date.PackStart(dateR, true);
        date.AddAttribute(dateR, "text", 3);

        // set the columns names
        title.Title = "Title";
        detail.Title = "Detail";
        price.Title = "Price";  
        date.Title = "Date";


        // append columns to the treeview       
        this.treeview.AppendColumn(title);
        this.treeview.AppendColumn(detail);
        this.treeview.AppendColumn(price);
        this.treeview.AppendColumn(date);


        // set the model
        this.treeview.Model = Singleton.Model.Instance.Data;    

    }
}

主窗口类

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

    protected void OnButtonOKClicked (object sender, System.EventArgs e){
        SumList list = new SumList();
        list.Show();
    }

    protected void onButtonHideClicked (object sender, System.EventArgs e){
        entrySum.Text = "";
        entryTitle.Text = "";
        this.Hide();
    }
}
4

1 回答 1

1

很简单,您的 GTK 控件正在收集垃圾。

public static void Main (string[] args)
{
    Application.Init ();
    new MainClass();

    Application.Run ();
}

您现在不再有任何对您的MainClass实例的实时引用。IMO,您很幸运该程序甚至可以做到这一点。

于 2012-05-10T20:34:12.977 回答