我有一个WPF应用程序,它包含一个长时间运行的主窗口。 在MainWindow构造函数中,按如下方式定义并初始化我的notifyIcon。
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("pack://application:,,,/APP;component/Resources/Logo.ico")).Stream)
{
System.Drawing.Icon defaultIcon = new System.Drawing.Icon(iconStream);
notifyIcon.Icon = defaultIcon;
BitmapImage image = new BitmapImage(new Uri("pack://application:,,,/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;
有时,通知图标只会从托盘中消失。没有代码,例如nofityIcon.Visible = false。 当我检查任务管理器时,我可以看到我的应用程序正在运行。
是否存在其他原因导致NotifyIcon行为不稳定以及补救措施?
答案 0 :(得分:1)
请检查系统托盘中的通知图标设置。控制面板 - >通知区域图标 - >选中复选框'始终显示所有图标' (Windows 7)。