Microsoft & .NETImplementing Windows Notification functionality using Silverlight 4.0

Implementing Windows Notification functionality using Silverlight 4.0

Introduction

Silverlight 4.0 SDK version provides support for implementing Windows notification functionality in Silverlight applications. In this article I will explain about the Windows notification functionality and the steps to implement it using a sample Silverlight application.

Windows Notification

Windows notification is nothing but the notifications which pop up via some applications at the bottom right corner of the computer screen just above the tray icons. For example the windows notification prompting for the system restart after the Windows update is complete. In a classic Windows-based application you would have to create a form and set its position accordingly. But Silverlight 4.0 provides support to display the content as a notification window.

NotificationWindow class in Silverlight 4.0

In Silverlight 4.0 a class named NotificationWindow is introduced under the System.Windows namespace. All the developer needs to do is to set the Content property of this NotificationWindow with the control which has to be shown as a notification.

Another important thing to note is that this will work only when the Silverlight application is running in Out-Of-Browser mode. To set the Silverlight application to run in out-of-browser mode, right click on the Silverlight application project and select properties. Check the control “Enable running application out of browser” as shown in Fig 1.0 below.

Image 1

Fig 1.0

Sample code

In this section we will create a sample application implementing the NotificationWindow functionality. Create a Silverlight application and add a user control named SampleNotification.xaml. Below is the XAML code for the user control.

<UserControl x_Class="NotificationWindowSample.SampleNotification"
    
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="120" d_DesignWidth="400" xmlns_sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">
    
    <Grid x_Name="LayoutRoot" Background="Orange">
        <sdk:Label Height="28" HorizontalAlignment="Left" Content="This is a sample Windows Notification" FontWeight="Bold" Margin="12,12,0,0" Name="label1" VerticalAlignment="Top" Width="241" />
        <sdk:Label Content="Enter text here: " Margin="0,0,41,0" /><TextBox Margin="98,44,158,41" /><Button Content="Submit" Margin="248,43,68,43" Click="Button_Click" />
        
    </Grid>
</UserControl>
namespace NotificationWindowSample
{
    public partial class SampleNotification : UserControl
    {
        public SampleNotification()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //Raise an evene and in the MainWindow subscribe to this event and have
            //needed implementation logic there.
        }
    }
}

Now in the MainWindow.xaml add a button control and implement the click event as shown below.

<UserControl x_Class="NotificationWindowSample.MainPage"
    
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d_DesignWidth="400">

    <Grid x_Name="LayoutRoot" Background="White">
        <Button Content="Show Notification" Margin="101,97,111,120" Click="Button_Click" />
    </Grid>
</UserControl>

namespace NotificationWindowSample
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        NotificationWindow notificationWindow = new NotificationWindow();
        notificationWindow.Content = new SampleNotification()
        {
            Width=400,
            Height=120
        };
        notificationWindow.Show(3000);
    }
    }
}

The Content of the NotificationWindow is set with SampleNotification control instance. Notice that the milliseconds value is passed to the Show method. This is to specify how long the notification should appear on the screen.

Set the Silverlight client application to run in Out-Of-Browser mode and run the application. Click on the Show Notification button and notice that the Notification Window appears as shown in Fig 2.0 below.

Image 2

Fig 2.0

Conclusion

I hope this article provided you with enough information to understand the new Silverlight 4.0 Windows Notification functionality and the steps involved in implementing it. Please make use of the comments section in case any further clarification is needed.

Happy reading!

Get the Free Newsletter!
Subscribe to Developer Insider for top news, trends & analysis
This email address is invalid.
Get the Free Newsletter!
Subscribe to Developer Insider for top news, trends & analysis
This email address is invalid.

Latest Posts

Related Stories