I’m having a very strange problem with statuicon.
I’m doing on a simple project to save and show some data in table, I have a mainwindow(MainWindow) where the user insert the data and then there is another window where are the data shown(SumList). Also there is a status icon which I have created by sublassing the Gtk.StatusIcon. The problem is that when I start the application and show the window that should show the data(everything works) and then close the window (no matter how) the statusIcon disappear from the panel.
Also I have found that it is cause be the length of constructor of the class SumList. If I delete some lines from there (random order) the statusicon works fine.
How can I fix this strange behavior?
EDIT #1
I try not to subclass the StatusIcon instead i have declared as static member of MainClass and now it works as it should, weird. Anyway the question is why it isn’t working if the statusIcon is not declared static?
The 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 ();
}
}
The SumList class
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;
}
}
The MainWindow class
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();
}
}
Simple, your GTK control is getting garbage collected.
You now no longer have any live references to your
MainClassinstance. IMO you are lucky that the program even does this.