Dear visitor, thanks for stopping by! If you want, you can follow all updates on Snowball.be via RSS. You can also follow me on Twitter or Facebook. More interesting posts from other Microsoft Regional Directors can be found at The Region.
Gill Cleeren         December 6, 2009    

Today is some kind of special day here in Belgium, it’s Saint Nicolas (aka Sinterklaar), comparable to Santa Claus. Special day or not, I’m ready for the sixth installment in my Silverlight article series. In this article, I’ll be describing how you can create toasts (aka notification windows) from Silverlight. We all know the notification windows from Outlook, those little windows that pop open when you receive a new mail. This is a new feature in Silverlight 4.

To support these windows, in Silverlight 4, a new class was added to instantiate them, namely the NotificationWindow. Notification windows are an out-of-browser feature only (just like some other new Silverlight 4 features such as the WebBrowser control which I covered earlier). To enable this behavior, go to the project properties of the Silverlight project and check the “Enable appliction running out-of-browser” checkbox.

image

Below is a screenshot of the sample code running.

image

Creating the notication window is straightforward. The following code runs when the “Try notification window” button is clicked in the interface:

private void NotificationButton_Click(object sender, RoutedEventArgs e)
{
    if (Application.Current.IsRunningOutOfBrowser)
    {
        NotificationWindow notificationWindow = new NotificationWindow();
        notificationWindow.Width = 300;
        notificationWindow.Height = 80;
        notificationWindow.Content = new TextBlock() 
            { Text = "Test notification window" };
        notificationWindow.Show(10000);
    }
    else
    {
        MessageBox.Show("You should take the application offline first");
    }
}

As you can see, we first perform a check to see if the application is running out-of-browser. Note that displaying a notification window does not need to be initiated from a user interaction, it can also be displayed following an application event (for example based on the Tick event of a Timer).

The notification window we created with the above code is very basic, as shown in the following screenshot.

image

Creating a more complex (and better looking) window is probably going to be required. And, the good news it’s quite easy to do. You can create a custom control or even a user control and set that as the Content for the NotificationWindow instance. In the code below, I added a third button and when clicked, a new, better looking window will be displayed.

The window itself is nothing more than a user control:

<Grid x:Name="LayoutRoot" Background="White">
    <Grid Width="350" Height="80">
        <Grid.RowDefinitions>
            <RowDefinition Height="15"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
        <Grid Background="DarkBlue">
            <TextBlock Text="Message" FontWeight="Bold"
                Foreground="Wheat"></TextBlock>
        </Grid>
        <Grid Grid.Row="1" Background="LightBlue">
            <TextBlock Text="You have clicked a button!"></TextBlock>
        </Grid>
    </Grid>

To display this control, we can execute the following code:

 

And this is the result:

image

  Posted on: Sunday, December 06, 2009 10:31:27 AM (Romance Standard Time, UTC+01:00)   |   Comments [0]
         
5/17/2012   4:52:20 PM
 Welcome to Snowball.be
Hello and welcome to snowball.be!

My name is Gill Cleeren, I'm a Microsoft Regional Director and an MVP ASP.NET.
On Snowball.be, you'll find all kind news and articles on .net, ASP.NET, WPF, Silverlight and Microsoft in general.
More on me can be found on my about page.

Should you have any questions, don't hesitate to contact me by Send mail to the author(s) .

 Partner sites
 Most popular tags
.net (124) .net 3.0 (6) .net 3.5 (18) .NET 4 (18) .NET Show (1) ADO.net (4) ASP.net (53) ASP.net AJAX (4) ASP.NET MVC (3) Atlas (12) Azure (2) Blend (2) Book (6) Book review (4) C# (43) Case studies (1) Chopsticks (3) Community (10) Community Day (16) Consoles (1) Database (1) DevDays09 (4) DotNetNuke (4) Efficiency (57) Enterprise Library (5) Events (60) Expression (7) Games (3) Hardware (9) Internet (18) IT (1) jQuery (1) LightSwitch (3) Links (11) LINQ (4) Mac (2) Metro (1) Microsoft (75) Mix 07 (6) Mix 08 (4) Mix 09 (1) Mix 11 (1) Movies (4) MVP (5) MVP Summit 2008 (3) mvvm (1) Office 2007 (10) Other (8) PDC (22) PDC2008 (10) Personal (36) ppt (9) Programming (52) Programming tools (22) Regional Director (2) Silverlight (143) Silverlight Advent Calendar (24) sl4 (44) SL5 Data and Services Cookbook (2) Slide decks (13) Snowball (13) Software (20) Microsoft (25) Speaking (14) SQL Server (10) TechDays (13) TechEd (14) telerik (6) Telerik (6) TFS (1) Twitter (1) Vista (73) Vista Tricks (9) Visual Studio.net (38) Visug (33) VS2010 (8) Wallpaper (2) WCF (2) Webcasts (9) Webinars (5) Windows (41) Windows 7 (5) Windows 8 (1) Windows Azure (2) Windows Mobile (3) Windows Phone 7 (2) WinFX (17) WinRT (1) WP7 (2) WPF (40) XAML (24)

 On this page
 This site
 Archives
Navigation
 Sitemap
 Blogroll OPML
 Disclaimer

All content is property of www.snowball.be. Nothing on this site can be copied or published elsewhere, unless otherwise stated.

This site is made by Gill Cleeren.

Questions? Opinions? Send mail to the author(s) E-mail