Why ‘User Control’ is Better Than ‘Notify’ in WPF

As we all know Notify icon is small pop up window which pops for any updates or any warnings. PopupImage

Notify icon control is readily available in WPF. Its implementation in the back end code is also pretty simple.

notifyIcon = new NotifyIcon();
 _notifyIcon.BalloonTipTitle = string.Format(Common.Properties.Resources.ApplicationMinimizeHelpText, _window.Title);
 _notifyIcon.MouseClick += notifyIcon_MouseClick;
 _notifyIcon.MouseDoubleClick += notifyIcon_MouseDoubleClick;
 _notifyIcon.BalloonTipClicked += notifyIcon_BalloonTipClicked;

But this Notify icon is suitable only to display short and crisp messages. Initially I too had implemented the same for my project. You can also write the required code in the events.

But the problem came when the client asked for more than single line message. They expected us to add a button, checkbox and image. Well initially I thought it is simple to add those. When browsed through the net, I came to know that Notify icon has its own constraints. It has limitation in the message length and anything you need to add, you add as list to the tray menu which is available on the right click of the Notify icon which was not meeting the client requirement.

_trayMenu = new ContextMenu();
 _trayMenu.MenuItems.Add("Open", (sender, args) => HandleSystemTrayVisibility(false));
 _trayMenu.MenuItems.Add("Exit", (sender, args) =>
 {
 _exitRequested = true;
 Application.Current.Shutdown();
 });
 _notifyIcon.ContextMenu = _trayMenu;

Then I came know the same can achieved by using User control. Using User control you can add anything, it is just another xaml page. With UI styling and control properties available you can make it look like Notify icon itself. So I would suggest to use the control which is helpful in implementing many things than Notify icon, if your requirement is more in terms of display.

_trayMenu = new ContextMenu();
 _trayMenu.MenuItems.Add("Open", (sender, args) => HandleSystemTrayVisibility(false));
 _trayMenu.MenuItems.Add("Exit", (sender, args) =>
 {
 _exitRequested = true;
 Application.Current.Shutdown();
 });
 _notifyIcon.ContextMenu = _trayMenu;
private void btnOk_Click(object sender, RoutedEventArgs e)
 {
 this.Close()
 }

This window can be called in required code as pop up

_notifyMessageView = new NotifyMessageView();
 _notifyMessageView.ShowInTaskbar = false;
 _notifyMessageView.Show();
 _notifyMessageView.Close();

Warning

Author

  • Rohini K

    Rohini Krishnamurthy is a senior software engineer at Trigent. She has more than 8 years of experience in .NET platform. She loves reading books and watching TV.