2015-04-15 57 views
1

我有一个WPF应用程序,它由长时间运行的主窗口组成。 在MainWindow构造函数中,定义并初始化我的notifyIcon,如下所示。Windows窗体NotifyIcon在WPF应用程序中随机消散

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行为,以及补救措施?

回答

1

请检查系统托盘中的通知图标设置。 ControlPanel->通知区域图标 - >选中“始终显示所有图标”复选框(Windows 7)。

+0

是的,我检查了所有的图标。该图标丢失。 –

+0

1.图标是否在启动应用程序时出现。 2.你能告诉我什么动作使图标不可见,双击,鼠标右键点击吗? 3. logoFile是否存在? – lerner1225

+0

在应用程序开始时会出现athe图标。我无法弄清楚它何时消失但随机消失。是logofile存在。从另一篇文章中,我了解到,与UI不同的一个不同的线程的notifyicon初始化会导致类似的错误。不幸的是,那也是我的情况。现在,我改变了逻辑,以便它仅在主UI线程上初始化。让我们看看它是怎么回事! –

相关问题