Gtk# 状态图标消失

本文关键字:消失 图标 状态图 状态 Gtk# | 更新日期: 2023-09-27 18:33:48

我在statuicon上遇到了一个非常奇怪的问题。

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

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

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

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

主类(状态图标(

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

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

Gtk# 状态图标消失

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

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

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