Created
October 12, 2025 19:16
-
-
Save dotMorten/f90fc13bd6f36af75eeb4fea11e78ccf to your computer and use it in GitHub Desktop.
Window-less TrayIcon app
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private TrayIcon icon; | |
| private Window? _window; | |
| private Window GetMainWindow() | |
| { | |
| if (_window is not null) | |
| return _window; | |
| _window = new MainWindow(); | |
| _window.AppWindow.Closing += (s, e) => | |
| { | |
| e.Cancel = true; | |
| s.Hide(); | |
| }; | |
| var wm = WindowManager.Get(_window); | |
| wm.WindowStateChanged += (s, state) => wm.AppWindow.IsShownInSwitchers = state != WindowState.Minimized; | |
| return _window; | |
| } | |
| protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args) | |
| { | |
| icon = new TrayIcon(1, "Images/StatusOK.ico", "Test"); | |
| icon.IsVisible = true; | |
| icon.LeftDoubleClick += (s, e) => GetMainWindow().Activate(); | |
| icon.RightClick += (w, e) => | |
| { | |
| var flyout = new MenuFlyout(); | |
| flyout.Items.Add(new MenuFlyoutItem() { Text = "Open" }); | |
| ((MenuFlyoutItem)flyout.Items[0]).Click += (s, e) => GetMainWindow().Activate(); | |
| flyout.Items.Add(new MenuFlyoutItem() { Text = "Quit App" }); | |
| ((MenuFlyoutItem)flyout.Items[1]).Click += (s, e) => | |
| { | |
| _window?.Close(); | |
| icon.Dispose(); | |
| }; | |
| e.Flyout = flyout; | |
| }; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment