public TrayIconViewModel(App app) { _theApp = app; _trayIcon = (TaskbarIcon) app.FindResource("TrayIcon"); if (_trayIcon == null) return; InTray = false; _trayIcon.DataContext = this; // TODO: Load from settings if (!_trayModeEnabled) { _trayIcon.Visibility = Visibility.Collapsed; _trayIcon.Dispose(); return; } var cm = new ContextMenu(); cm.Items.Add(new MenuItem { Header = "Exit Tray Sample App", Command = ExitApplication }); _trayIcon.ContextMenu = cm; _theApp.MainWindow.Closing += MainWindow_Closing; }
private void CreateSystrayIcon() { if (ProgramSettings.Instance.SystrayDisplay) { if (MyNotifyIcon == null) { try { MyNotifyIcon = new TaskbarIcon() { Name = "MyNotifyIcon", VerticalAlignment = System.Windows.VerticalAlignment.Top, Icon = new System.Drawing.Icon(Application.GetResourceStream(new Uri("pack://application:,,,/CIV;component/Icons/Application.ico")).Stream), ContextMenu = new ContextMenu() }; MyNotifyIcon.DoubleClickCommand = new DashboardShowCommand(); MyNotifyIcon.DoubleClickCommandParameter = this; MyNotifyIcon.SetBinding(Hardcodet.Wpf.TaskbarNotification.TaskbarIcon.ToolTipTextProperty, new Binding("Text.Dashboard_Title") { Source = new CIVResourceManager() }); // Menu MenuItem menu = new MenuItem() { Icon = new Image() { Source = new BitmapImage(new Uri("pack://application:,,,/CIV;component/Images/Exit.png", UriKind.RelativeOrAbsolute)), HorizontalAlignment = System.Windows.HorizontalAlignment.Center, VerticalAlignment = System.Windows.VerticalAlignment.Center, Stretch = Stretch.None } }; menu.Click += miExit_Click; menu.SetBinding(MenuItem.HeaderProperty, new Binding("Text.Dashboard_miExit") { Source = new CIVResourceManager() }); MyNotifyIcon.ContextMenu.Items.Add(menu); // Contenu Style style = new Style(typeof(ToolTip), null); style.Setters.Add(new Setter(ContentControl.TemplateProperty, new ControlTemplate(typeof(ToolTip)) { VisualTree = new FrameworkElementFactory(typeof(ContentPresenter)) })); ToolTip tip = new ToolTip() { Style = style, Content = new QuickStats() }; MyNotifyIcon.TrayToolTip = tip; } catch (Exception e) { LogEngine.Instance.Add(e, false); } } } else { if (MyNotifyIcon != null) { try { MyNotifyIcon.Icon.Dispose(); MyNotifyIcon.Dispose(); MyNotifyIcon = null; } catch (Exception e) { LogEngine.Instance.Add(e, false); } } } }