Search code examples
c#wpftrayicontray

WPF C# Tray Icon Implementation Issue


I've faced the issue while trying to implement a tray icon. App was instantly closing every time I launched it. So first thing first I decided to create a new project to try it there, just in case if I messed something up in my app. But it proceeded to close. In this new project, I just removed StartupUri and added this piece of code. One more important thing - If we comment the line with Drawing.Icon the window doesn't close after running despite any tray options.

        protected override void OnStartup(StartupEventArgs e)
        {
            MainWindow = new MainWindow();
            MainWindow.Show();

            Forms.NotifyIcon _notifyIcon = new Forms.NotifyIcon();
            _notifyIcon.Icon = new System.Drawing.Icon("icon.ico");
            _notifyIcon.Visible = true;

            base.OnStartup(e);
        }

I would be extremely grateful if you could help me to figure it out


Solution

  • It was very simple. I was trying to import WinForms for WPF. Here is everything explained about Notifyicon in WPF. Thanks to Andy