mercredi 15 avril 2015

Windows Forms NotifyIcon disappers randomly in WPF application

I have a WPF application that consists of a long running main window. In the MainWindow constructor a define and initialize my notifyIcon as follows.



notifyIcon = new System.Windows.Forms.NotifyIcon();
if (File.Exists(logoFile))
{
BitmapImage image = new BitmapImage(new Uri(logoFile, UriKind.Absolute));
this.Icon = image;
notifyIcon.Icon = new System.Drawing.Icon(logoFile);
this.gridAbout_imgLogo.Source = image;
}
else
{
using (Stream iconStream = Application.GetResourceStream(new Uri("http://packapplication:,,,/APP;component/Resources/Logo.ico")).Stream)
{
System.Drawing.Icon defaultIcon = new System.Drawing.Icon(iconStream);
notifyIcon.Icon = defaultIcon;
BitmapImage image = new BitmapImage(new Uri("http://packapplication:,,,/APP;component/Resources/Logo.ico"));
this.Icon = image;
this.gridAbout_imgLogo.Source = image;
}
}
if (config != null)
notifyIcon.Text = config.app_name;
else
notifyIcon.Text = "APP";
notifyIcon.DoubleClick += notifyIcon_DoubleClick;
notifyIcon.Visible = true;
System.Windows.Forms.ContextMenu m = new System.Windows.Forms.ContextMenu();
System.Windows.Forms.MenuItem mi = new System.Windows.Forms.MenuItem();
mi.Text = "Show App Status";
mi.Click += (s, e) => ShowApplication();
m.MenuItems.Add(mi);

# if DEBUG
System.Windows.Forms.MenuItem mi1 = new System.Windows.Forms.MenuItem();
mi1.Text = "Exit";
mi1.Click += (s, e) => Application.Current.Shutdown();
m.MenuItems.Add(mi1);
# endif

notifyIcon.ContextMenu = m;


Sometimes notify icon simply disappeears from the tray. No code such as nofityIcon.Visible = false. When I check the task manager I can see my app is running.


Are there other reasons for flaky NotifyIcon behavior, as well as remedies?





Aucun commentaire:

Enregistrer un commentaire