Can Of Code

WPF / C# notifcations pop up

There becomes a point in a lot of applications where you hit the notification wall. I have recently been working on an RSS Reader to practice some concepts that are covered in the MCTS (Windows Application Development 70-511) exam.

I got the application working with data bindings and all the other WPF Wizardry and decided to start using it. I currently use Outlook to read RSS Feeds so I thought it would be worth seeing if what I had created was any better.  I stumbled across a real issue for me. It didn’t notify me when it had updated. This isn’t really a big issue is it? Well it really was, because I was hooked to applications telling me when something has happened for example, email, tweetdeck, MSN Messenger etc. Like below.

TweetDeck Notification window

A prime example of a notification window

 

Rather then give up with my application I thought, why not add my own notifications to it! So here is a little guide on how to add a notification popup to your application.

 

the form

we start by creating a Window. The Window will be what is displayed when the notification happens.

WPF Notification Popup

 

There are a few properties within the window we need to set to make it look right. Here is the XAML snippet for window

<Window x:Class="RSSBuddy.notificationWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="notificationWindow"  ResizeMode="NoResize" ShowInTaskbar="False"
        Topmost="True" WindowStyle="None" HorizontalAlignment="Right"
        VerticalAlignment="Bottom">

 

So lets explain some of the things we are doing:

ResizeMode = NoResize

This means the user wont be able to resize the notification window, which is fine because why would they?

ShowInTaskbar = False

We don’t want this window to have an icon in the taskbar because its not really a window people will see for long.

Topmost = True

This property is important to our notification window. The property forces the window to the front of the screen in front of any open applications. If we didn’t set this to true the notification window will end up getting lost behind other applications.

WindowStyle = none

This property means that we don’t have the border around the window or the close button and title. We set this to none so we can do our own title bar that fits better to the application.
HorizontalAlignment= Right & VerticalAlignment= Bottom

Both these properties are in charge of placing the window in the bottom right corner.

 Wait a minute…

If you create your notification window and run it, something bizarre will happen. Your notification window will appear not in the bottom right of the screen where you wanted it. This happens because it positions itself at the bottom right of its parent window NOT the screen. The way to overcome this was to tweak the code before the opening of the window:

  note = new notificationWindow(newitemcount);
  note.Top = main.ActualHeight - note.Height - 15;
  note.Left = main.ActualWidth - note.Width - 30;
  note.Show();

 

This code is what I use to open the notification window when something has happened. firstly we create the notification window object. Now to get over the problem of the misplacement. I go and manually set the notification windows Top and Left Properties. I set these by getting my parent screens ActualHeight (parent screen is set to Maximised by default) and taking away the Height of my notification window and then and extra 15 for good measure. This results in the notification window appearing where it should in the bottom right corner.

NOTE: If your parent screen is not Maximized you could use the following instead of the parent windows ActualHeight and ActualWidth (thanks to: StackOverflow Answer)

System.Windows.SystemParameters.PrimaryScreenWidth
System.Windows.SystemParameters.PrimaryScreenHeight

 

Comments

Leave a Reply

Your email address will not be published.