November 27, 2014
Hot Topics:

Implementing Windows Notification functionality using Silverlight 4.0

  • June 7, 2011
  • By Arun Karthick
  • Send Email »
  • More Articles »

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="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    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="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    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!


Tags: tutorial, Microsoft Silverlight, Windows Notification, Silverlight 4.0

Originally published on http://www.developer.com.


Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Rocket Fuel