WPF Application that only has a tray icon
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
A WPF application can run entirely from the Windows notification area without showing a main window at startup. The usual implementation uses System.Windows.Forms.NotifyIcon for the tray icon and configures the WPF application so it does not shut down just because no normal window is open.
The Core Design
A tray-only WPF app usually needs three pieces:
- no
StartupUriinApp.xaml - '
ShutdownMode="OnExplicitShutdown"' - a
NotifyIconwith a context menu and an explicit exit path
If you leave WPF in its default shutdown mode, the app often exits immediately because there is no visible main window keeping it alive.
Configure App.xaml
Remove StartupUri and set the shutdown mode explicitly.
This tells WPF that the application lifetime will be managed in code.
Create the Tray Icon in App.xaml.cs
The tray icon comes from Windows Forms, not native WPF controls.
This pattern starts the app without showing a main window, but still gives the user a tray icon to interact with.
Hide Instead of Closing
If the app has a normal window that users can reopen from the tray, it is common to hide the window instead of destroying the whole application when they click the close button.
This keeps the process alive and lets the tray icon reopen the window later.
Important Cleanup
Always dispose the NotifyIcon before shutting down. If you do not, Windows can leave a ghost tray icon behind until the user hovers over it or Explorer refreshes the tray.
That is one of the most common "it mostly works" bugs in tray-only desktop apps.
Alternative Libraries
Some teams prefer wrapper libraries such as Hardcodet's taskbar-notification helpers because they integrate more naturally with WPF patterns. The underlying idea is still the same: WPF itself does not provide a first-class tray icon control, so you either wrap NotifyIcon yourself or use a helper library.
For a small app, the direct NotifyIcon approach is often enough.
Common Pitfalls
The biggest mistake is forgetting to change ShutdownMode. Without OnExplicitShutdown, a tray-only app may terminate as soon as there is no open WPF window.
Another issue is leaving StartupUri set, which causes a window to appear even though the app is supposed to live only in the tray.
Developers also forget to dispose the tray icon on exit, which leaves stale icons in the notification area.
Finally, if you hide the main window on close, make sure the user still has an obvious way to exit the application from the tray menu.
Summary
- A tray-only WPF app usually uses
NotifyIconfromSystem.Windows.Forms. - Set
ShutdownMode="OnExplicitShutdown"and removeStartupUri. - Create the tray icon in
App.xaml.csand provide a context menu with an exit command. - Hide the main window instead of closing it if the app should keep running in the tray.
- Dispose the tray icon cleanly before calling
Shutdown().
Related reading
- WPF background operations using Asynchronous Workflows
- WPF Data Binding and Validation Rules Best Practices
- WPF global exception handler
- WPF Loading animation
- WPF MVVM How to close a window
- WPF TextBox won't fill in StackPanel
- WPF updating the itemssource in a ListBox with an async method
- WPF User Control Parent

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.