First time here?

You are looking at the most recent posts. You may also want to check out older archives. Please leave a comment, ask a question and consider subscribing to the latest posts via RSS or email. Thank you for visiting!

One of the cool features for Silverlight 4 Out-of-browser applications that was announced as a part of the Silverlight 4 beta release is the NotificationWindow class.  Most commonly referred to as “toast” these are the little notifications that popup in the system areas of your operating system to provide helpful (and sometimes annoying) information.  Any user of Microsoft Outlook no doubt has seen them.  They look something like this (Windows system tray area shown here):

Silverlight 4 NotificationWindow sample image

After reading Corey’s post showing a quick FAQ on customizing the experience of the NotificationWindow, I was drawn to the comment he made about queuing.  While the NotificationWindow API doesn’t have a queuing mechanism, .NET does and we can leverage that.

NOTE: For a simple overview with a short video and downloadable sample code in C# and Visual Basic about the Notification Window API visit this video: Notification API in Silverlight 4.

Let’s say you had some need to queue up notifications.  Or better yet, you really didn’t have a *need* to, but wanted to ensure that any notification you wanted to display would regardless of if a notification was currently showing, etc. – you want to make sure your notification hits.

Instead of just firing .Show() on your NotificationWindow, thing of using the Queue<T> class.  This enables a collection of items stored in sequential order and serves this purpose well.  Let’s assume a simple demonstration here.

We have an extremely simple UI with four buttons…each going to trigger a different NotificationWindow.  Here’s our XAML:

   1: <StackPanel x:Name="ApplicationSample" Visibility="Collapsed">
   2:     <Button Content="Notification One" x:Name="Notify1" Click="Notify1_Click" />
   3:     <Button Content="Notification Two" x:Name="Notify2" Click="Notify2_Click" />
   4:     <Button Content="Notification Three" x:Name="Notify3" Click="Notify3_Click" />
   5:     <Button Content="Notification Four" x:Name="Notify4" Click="Notify4_Click" />
   6: </StackPanel>

The button click logic looks the same for each (with the exception of passing in a different string of text to display):

   1: private void Notify1_Click(object sender, RoutedEventArgs e)
   2: {
   3:     CreateNewNotificationControl("Notification One");
   4: }
   6: private void CreateNewNotificationControl(string NotificationText)
   7: {
   8:     NotificationWindow nw = new NotificationWindow();
   9:     nw.Width = 400;
  10:     nw.Height = 100;
  11:     nw.Closed += new EventHandler<EventArgs>(OnNotificationClosed);
  13:     NotificationControl nc = new NotificationControl();
  14:     nc.NotificationText.Text = NotificationText;
  15:     nw.Content = nc;
  17:     AddNotificationToQueue(nw);
  18: }

Notice the core function of CreateNewNotificationControl and the last line of AddNotificationToQueue.  This is the key function here.  Here is the code for the function:

   1: private void AddNotificationToQueue(NotificationWindow notification)
   2: {
   3:     if (_currentWindow == null)
   4:     {
   5:         _currentWindow = notification;
   6:         notification.Show(5000);
   7:     }
   8:     else
   9:     {
  10:         _notifyQueue.Enqueue(notification);
  11:     }
  12: }

In the code above _currentWindow is a NotificationWindow and _notifyQueue is a Queue<NotificationWindow> member variable.  We basically are adding the NotificationWindow objects we are creating to this collection and letting the Queue<T> help us manage what is showing when.  It is also key to note that when we do add a new NotificationWindow that we are attaching a Closed event handler that helps trigger the Dequeue function for our collection.

The end result is that we are essentially queuing up our notification windows.  Here’s a simple result of the code running (animated image):

NotificationWindow Queue demonstration

You can download the full code here: NotificationWindowQueue_CS.zip (C#) or NotificationWindowQueue_VB.zip (Visual Basic).  Remember, that NotificationWindow requires the application to run in Out-of-browser mode, so be sure to install it first!  Also, this requires Visual Studio 2010 and Silverlight 4 development tools to run.

This concept might come in useful for business applications where different functions (perhaps things out of view) need to notify the user of certain things.  Hope this helps think outside the box a little bit!

This work is licensed under a Creative Commons Attribution By license.

11/22/2009 9:49 PM | # re: Silverlight 4 NotificationWindow and Queuing multiple notifications
Tim, I'm beginning to get concern about certain important and valuable features that are OOB "Only". Conceptually, when I want offer a feature to the user, I always have to assume that, the user may have not opted to make the app as "OOB" and they are still running in the browser only. (Many people have fear of installing apps on their machine, after so many bad people have abused that in the past).
So, this brings up the point that some of these new features in SL4 that are really nice seem to be available for OOB only and that can actually cause a problem if a developer is depending on that feature and the user has not installed it as OOB. How do we go about this?

11/22/2009 9:52 PM | # re: Silverlight 4 NotificationWindow and Queuing multiple notifications
Ben -- yes, there is a dependency on the developer knowing what they can/can't do. But there are also facilities in the API to help the developer avoid pitfalls. If the dev knows one feature they want to implement is OOB only, they can check App.Current.IsRunningOutOfBrowser for instance. If they are doing COM interop they can check ComAutomationFactory.IsAvailable. These types of things are available to help the developer create the best user experience without throwing exceptions :-)
11/22/2009 10:08 PM | # re: Silverlight 4 NotificationWindow and Queuing multiple notifications
That's exactly what I was thinking. Discretion must be used!

When checking the "IsRunningOutOfBrowser", should we also pair that with "HasElevatedPernission" condition as well?

11/22/2009 10:09 PM | # re: Silverlight 4 NotificationWindow and Queuing multiple notifications
Ben -- absolutely...if you are doing something elevated it would be good to check that first as well.
11/23/2009 12:06 AM | # re: Silverlight 4 NotificationWindow and Queuing multiple notifications
Is it possible to do shaped notification windows? I notice that the example has a large white area where the content doesn't completely fill it. Would that appear if the content had, for example, a rounded border?
11/25/2009 12:32 AM | # re: Silverlight 4 NotificationWindow and Queuing multiple notifications
David -- you can't to rounded corners/transparencies in this iteration.
11/30/2009 8:06 AM | # re: Silverlight 4 NotificationWindow and Queuing multiple notifications
In looking at some of the upcoming OOB support with SL4, namely custom Chrome (i know not supported in current beta), I was wondering if you could tell us whether we might be able to send a SL4 OOB application to the System Tray instead of the application Bar.
Basically we are looking at a helper app which needs to communicate with our web application, raise notifications as per your article, and a SL4 OOB app would be a perfect match for running across both the MAC and the PC. Ideally we would want this app to run on start up, and sit in the application tray.

Do you know if SL4 will allow this type of customization/implementation ?
11/30/2009 9:55 AM | # re: Silverlight 4 NotificationWindow and Queuing multiple notifications
NicS - System tray is not currently on the short list for SL4. But you can vote on it: silverlight.uservoice.com/.../311990-minimize-t... to let the team know how important it is.
7/28/2011 5:36 AM | # re: Silverlight 4 NotificationWindow and Queuing multiple notifications
Thank you very much, that's the code I'm looking for!

Please add 8 and 8 and type the answer here:


The opinions/content expressed on this blog are provided "ASIS" with no warranties and are my own personal opinions/content (unless otherwise noted) and do not represent my employer's view in any way.