You can use the following with your main window:
PHP Code:
namespace YourApplicationName.View
{
using System;
using System.Windows;
using System.Windows.Forms;
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow
{
/// <summary>
/// Notification icon object.
/// </summary>
private readonly NotifyIcon m_NotifyIcon;
/// <summary>
/// Default Constructor
/// </summary>
public MainWindow()
{
this.InitializeComponent();
try
{
// Attempt to obtain the icon stream..
var iconUri = new Uri("/YourApplicationName;component/Assets/appicon.ico", UriKind.RelativeOrAbsolute);
var iconStream = System.Windows.Application.GetResourceStream(iconUri);
if (iconStream == null)
return;
// Prepare the notify icon..
this.m_NotifyIcon = new NotifyIcon
{
Icon = new System.Drawing.Icon(iconStream.Stream),
Text = "YourApplicationName",
Visible = true
};
// Handle double click event..
this.m_NotifyIcon.MouseDoubleClick += (sender, args) =>
{
this.WindowState = this.WindowState == WindowState.Normal ? WindowState.Minimized : WindowState.Normal;
this.ShowInTaskbar = this.WindowState != WindowState.Minimized;
};
// Register to window events..
this.Closed += this.OnClosed;
this.StateChanged += this.OnStateChanged;
}
catch (Exception)
{
}
}
/// <summary>
/// Closed event callback.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnClosed(object sender, EventArgs e)
{
if (this.m_NotifyIcon != null)
{
this.m_NotifyIcon.Visible = false;
this.m_NotifyIcon.Dispose();
}
}
/// <summary>
/// StateChanged event callback.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnStateChanged(object sender, EventArgs e)
{
// Handle minimized state changes for tray icon..
if (this.m_NotifyIcon != null)
this.ShowInTaskbar = this.WindowState != WindowState.Minimized;
}
}
}