【发布时间】:2015-04-15 12:44:16
【问题描述】:
我有一个 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 异常行为的原因以及补救措施吗?
【问题讨论】:
标签: wpf windows notifyicon