WPF Application that only has a tray icon
There's no NotifyIcon for WPF.
A colleague of mine used this freely available library to good effect:
- http://www.hardcodet.net/wpf-notifyicon (blog post)
- https://github.com/hardcodet/wpf-notifyicon (source code)
- https://www.nuget.org/packages/Hardcodet.NotifyIcon.Wpf/ (NuGet package)
- http://visualstudiogallery.msdn.microsoft.com/aacbc77c-4ef6-456f-80b7-1f157c2909f7/
How to create WPF System Tray Icon when no Main host window exists
Set the application ShutdownMode
to OnExplicitShutdown
and display the tray icon from the Application.OnStartup
. This example uses the NotifyIcon
from WinForms
, so add a reference to System.Windows.Forms.dll
and System.Drawing.dll
. Also, add an embedded resource for the Tray Icon.
App.xaml
<Application x:Class="WpfTrayIcon.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
ShutdownMode="OnExplicitShutdown"
>
<Application.Resources>
</Application.Resources>
</Application>
App.xaml.cs
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Windows;
using NotifyIcon = System.Windows.Forms.NotifyIcon;
namespace WpfTrayIcon
{
public partial class App : Application
{
public static NotifyIcon icon;
protected override void OnStartup(StartupEventArgs e)
{
App.icon = new NotifyIcon();
icon.Click += new EventHandler(icon_Click);
icon.Icon = new System.Drawing.Icon(typeof(App), "TrayIcon.ico");
icon.Visible = true;
base.OnStartup(e);
}
private void icon_Click(Object sender, EventArgs e)
{
MessageBox.Show("Thanks for clicking me");
}
}
}
WPF C# Tray Icon Implementation Issue
It was very simple. I was trying to import WinForms for WPF.
Here is everything explained about Notifyicon in WPF.
Thanks to Andy
Minimizing Application to system tray using WPF ( Not using NotifyIcon )
If you'll reconsider your reluctance to using an external component, I recommend WPF NotifyIcon. I've used it. It's straightforward and works well.
It does not just rely on the corresponding WinForms component, but is a purely independent control which leverages several features of the WPF framework in order to display rich tooltips, popups, context menus, and balloon messages.
wpf c# tray icon app terminates after 30 minutes
This line is causing a memory leak:
hIcon = (bitmapText.GetHicon());
hIcon needs to be destroyed:
hIcon = (bitmapText.GetHicon());
ni.Icon = System.Drawing.Icon.FromHandle(hIcon);
DestroyIcon(hIcon);
Add this code to the class to define DestroyIcon:
[DllImport("user32.dll", SetLastError = true)]
static extern bool DestroyIcon(IntPtr hIcon);
see:
https://msdn.microsoft.com/en-us/library/system.drawing.icon.fromhandle(v=vs.110).aspx
Related Topics
How to Find Multiple Occurrences with Regex Groups
Enforce an Async Method to Be Called Once
C#: Passing Null to Overloaded Method - Which Method Is Called
Custom Deserialization Using JSON.Net
Show Loading Animation During Loading Data in Other Thread
Webclient.Downloadstring() Returns String with Peculiar Characters
How to Unregister 'Anonymous' Event Handler
How Does the .Tostring() Method Work
Drawing Glitches When Using Creategraphics Rather Than Paint Event Handler for Custom Drawing
Standardoutput.Readtoend() Hangs
Serialization of Entity Framework Objects with One to Many Relationship
Convert Arabic"Unicode" Content HTML or Xml to PDF Using Itextsharp
Establish a Link Between Two Lists in Linq to Entities Where Clause
How to Multi-Target a .Net Core Class Library with Csproj
C# Covariant Return Types Utilizing Generics