Use of Notification Tray Icons in .NET 8.0 in WPF

A system tray icon, also referred to as a notification tray icon or taskbar notification icon, is a compact visual representation that appears in the notification area of a desktop's taskbar or system tray. Its primary purpose is to offer convenient access to the functionality of applications or to present notifications to the user.

On Windows operating systems, the notification area is situated in the lower right corner of the taskbar. It commonly showcases icons that represent different system utilities, background processes, and active applications. Users can engage with these icons by clicking on them to access specific features or settings associated with each.

Implementation

Step 1. Create a WPF window-based application like below

WPF

Step 2. Create a Behavior class named "NotifyTrayIconBehavior.cs" following the structure provided below.

using Microsoft.Xaml.Behaviors;
using System;
using System.Windows;
using System.Windows.Forms;

namespace WpfNotifyIconExample.Behaviors
{
    public class NotifyTrayIconBehavior : Behavior<Window> //Attach behavior with window
    {
        private NotifyIcon? notifyTrayIcon;

        protected override void OnAttached()
        {
            base.OnAttached();
            AssociatedObject.Loaded += AssociatedObject_Loaded;
            AssociatedObject.Closing += AssociatedObject_Closing;
            AssociatedObject.StateChanged += AssociatedObject_StateChanged;
        }

        protected override void OnDetaching()
        {
            base.OnDetaching();
            AssociatedObject.Loaded -= AssociatedObject_Loaded;
            AssociatedObject.Closing -= AssociatedObject_Closing;
            AssociatedObject.StateChanged -= AssociatedObject_StateChanged;
        }

        private void AssociatedObject_Loaded(object sender, RoutedEventArgs e)
        {
            InitializeNotifyIcon();
        }

        private void AssociatedObject_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = true;
            AssociatedObject.Hide();
            ShowNotificationInTray("", "This balloon indicates that you can access your application from the system tray icon.");
        }

        private void AssociatedObject_StateChanged(object sender, EventArgs e)
        {
            if (AssociatedObject.WindowState == WindowState.Minimized)
            {
                AssociatedObject.ShowInTaskbar = false;
                ShowNotificationInTray("", "This balloon indicates that you can access your application from the system tray icon.");
            }
            else
            {
                AssociatedObject.ShowInTaskbar = true;
            }
        }

        private void InitializeNotifyIcon()
        {
            // Initialize NotifyIcon
            notifyTrayIcon = new NotifyIcon
            {
                Icon = GetIconFromImageSource(new Uri("pack://application:,,,/WpfNotifyIconExample;component/TrayIcon.ico")), //Specify the icon to appear in the notification area.
                Visible = true,
                ContextMenuStrip = new ContextMenuStrip()
            };

            //// Add a context menu to the NotifyIcon
            ContextMenuStrip contextMenu = new();
            ToolStripMenuItem exitNotifyIconMenuItem = new()
            {
                Text = "Exit"
            };
            ToolStripMenuItem openNotifyIconMenuItem = new()
            {
                Text = "Open App"
            };

            openNotifyIconMenuItem.Click += OpenMenuItem_Click;
            exitNotifyIconMenuItem.Click += ExitMenuItem_Click;
            contextMenu.Items.Add(openNotifyIconMenuItem);
            contextMenu.Items.Add(exitNotifyIconMenuItem);
            notifyTrayIcon.ContextMenuStrip = contextMenu;
        }

        private void ExitMenuItem_Click(object? sender, EventArgs e)
        {
            // Perform cleanup and exit the application
            notifyTrayIcon?.Dispose();
            System.Windows.Application.Current.Shutdown();
        }

        private void OpenMenuItem_Click(object sender, EventArgs e)
        {
            AssociatedObject.Show();
            AssociatedObject.WindowState = WindowState.Normal;
            AssociatedObject.Activate();
        }

        private void ShowNotificationInTray(string title, string message)
        {
            //To showcase a balloon tip, utilize the notifyTrayIcon function and specify the duration (2000 milliseconds), title, message, and icon type (ToolTipIcon.Info).
            notifyTrayIcon.ShowBalloonTip(2000, title, message, ToolTipIcon.Info);
        }

        private Icon? GetIconFromImageSource(Uri uri)
        {
            using var stream = System.Windows.Application.GetResourceStream(uri)?.Stream;
            return stream != null ? new Icon(stream) : null;
        }
    }
}

Step 3. Attach the mentioned behavior class to the main form of the application, as demonstrated in the example below.

Behavior class

Step 4. The presence of the "Notification tray icon" when an application is running.

Notification tray icon

Repository Path: https://github.com/OmatrixTech/WpfNotifyIconExample

Next Recommended Reading Dynamic Menu with Icon in WPF