Pixata Custom Controls
For Lightswitch

Recent Posts

Popular tags (# posts in brackets)

Anonymous types (3) ASP.NET (5) C# (3) C# tricks and tips (2) Computers (6) Design patterns (3) DomainDataSource (3) Dynamic data (4) Entity framework (3) Entity model framework (5) F# (3) LightSwitch (12) Linq (6) Microsoft (2) MVP (2) MVVM (2) Project Euler (2) RIA services (5) Silverlight (2) SQL Server (4) Unit testing (4) Visual Studio (7) WCF (3) WPF (3)

Gratuitous link to StackExchange




The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

Actually, as I'm self-employed, I guess that means that any views I expressed here aren't my own. That's confusing!


Theme modified from one by Tom Watts
C#/F# code styling by Manoli (for posts pre-2016) and Google code prettify (for post from Jan 2016 and beyond)

My rambling thoughts on exploring the .NET framework and related technologies

I have just updated my Pixata Custom Controls extension for Lightswitch. Version 1.10 contains what I believe is the very first notification toast implementation for Lightswitch. As its usage differs from other controls, and it allows significantly more customisation than the others, I decided to write a blog post about it.

The simple usage of the control is to allow you to display a small notification window in the lower right-hand corner of the user’s screen (upper right-hand if they are using a Mac), which will not interfere with whatever they are doing, and will quietly go away after a short time. They are officially called “notification windows” but are colloquially known as toast, due to the way they pop up. One of the most famous uses of toast is when Microsoft Outlook informs you that you have a new email. This is a very user-friendly way of doing this, and is far better than the evil message boxes that plagued user interfaces for decades. If you still things message boxes are OK, please read some more about why they are evil and stop using them!

Imagine you were writing an application that maintained a list of ferrets (well, don’t we all?), and you wanted to notify the user when a new ferret had been added to the database. Instead of annoying them with a message box, you could simply pop up a notification like this...

A notification toast

The user can either ignore it, or click the close button to dismiss it.

I thought it would be really nice if we could have these in Lightswitch, and devoted the whole of today to developing a control to do just that.

Before I continue though, it’s worth pointing out that due to a limitation in the Silverlight API, toasts will only work in Out-Of-Browser applications. If you attempt to use this control in a browser, nothing will happen. Also, according to the documentation, you can’t have any interactive controls on the toast, although it was quite easy to add the close button (look at the top-right of the toast in the picture above), so I’m not sure that this is as restrictive as it sounds. Finally, the API does not give any control of the fade in/out duration, the shape of the toast (so no rounded corners), or the way the toast appears (eg sliding in as opposed to fading in), so we are stuck with the default behaviours in these respects. Having said that, there’s still a lot you can do with them, as I hope to show. With that aside, let’s see how to use the control.

Displaying Toast

If you have the latest version of the Pixata Custom Controls extension for Lightswitch, then all you need to do to display toasts is to switch to the code behind of the screen where you want the notification to appear. At the top of the file, add the following line...

using PixataCustomControls.Presentation.Controls;

You don’t need to worry about adding a reference to the controls, if you’ve enabled them in the Lightswitch project properties, then the reference will already be there.

To display the toast, you can do something as simple as this...

  PixataToastHelper.ShowToast("Title", "Type your message here");

This will display a fairly plain toast, using the title and message you supplied. Obviously, you can modify these to suit your needs, including data from the Lightswitch application.

If you want something a little more interesting, you can customise the toast by adding the following parameters to the call to ShowToast...

Parameter Meaning
ToastStyle There is an enumeration in the control that gives you five more styles for the toast. These are Info, Warning, Alert, Error and Elephant. The more observant readers may notice that the last one is a little out of place. This is because I was creating a custom style to show how easy it is to add your own (see below), and decided to leave it in as it amused me.

If you change the call to the following, you’ll end up with a toast that looks like the one shown above...

  PixataToastHelper.ShowToast("Ferret Information", "New ferret added 6th March '12, by user Freddy",
Notice that last parameter? That sets one of the six predefined styles (including the default Plain style), and will be suitable for most purposes.
TimeOut This integer value specifies for how milliseconds the toast will be displayed before it disappears.

Due to limitations in the .NET API, this value cannot be less than zero (I believe Microsoft are working on time travel, but I don’t think it will be available until .NET 5 comes out), nor greater than 30000. Any values outside of this range will be modified to be between zero and 30000.
Width and Height These are pretty self-explanatory. Again, due to API limitations, toasts cannot be wider than 400 pixels, nor higher than 100 pixels. If you enter a value less than zero or greater than these maximum values, it will be modified.

That’s basically all there is to the standard usage. As you can see, very easy to use, but very effective.

However, you might look at these and say “I don’t like your designs, I want it different.” Well, that’s absolutely fine, because I added the capability for you to create your own toast styles! It’s a little more involved than the standard usage, but not much.

Creating Your Own Toast

The first thing you’ll need to do is to add a new Silverlight User Control to your Client project. For the purposes of this blog post, I’m going to show you how I built the Elephant style. Truth is, the only reason that style ended up in the extension is because once I had built it, I liked it enough to keep it! So, imagine you have called your control MyToast, although why you would do that is beyond me. I have no idea why Microsoft think that we all want our folders called My Documents, and My Viruses and so on. Anyway, I had a senior moment and called my sample control MyToast, so you’re going to have to live with that for this tutorial.

You will need to make a few changes to the control before you start designing your toast. Without boring you with the details, any toast control needs to be derived from a base control I wrote for the purpose. This allows you to pass the toast into the ShowToast method. At the top of the XAML, add a reference to the Pixata Custom Controls namespace, and then change the top-level tag from UserControl to uc:PixataToastControlBase as follows...

<uc:PixataToastControlBase x:Class="LightSwitchApplication.MyToast"

I also like to add a design width and height, as these make it easier to work with when you’re designing the toast. The end of your opening tag should look like this...


Now switch to the control’s code behind, and have the control inherit from PixataToastControlBase instead of UserControl. You’ll need to add the following using at the top of the file...

using PixataCustomControls.Presentation.Controls;

...and then change the class declaration as follows...

  public partial class MyToast : PixataToastControlBase {

At this stage, you can now happily design your toast. If you want to play along, here is the XAML for my elephant control...

<uc:PixataToastControlBase x:Class="LightSwitchApplication.MyToast"
      <ColumnDefinition Width="*" />
      <ColumnDefinition Width="100" />
    <StackPanel Orientation="Vertical"
      <TextBlock Name="Title"
                 Text="The Title"
                 Foreground="White" />
      <Rectangle HorizontalAlignment="Stretch"
                 Height="1" />
      <TextBlock Name="Message"
                 Text="The stuff on the toast that blathers on about nothing"
                 TextWrapping="Wrap" />
    <Button Grid.Column="1"
        <Image Width="100"
               Source="/TestingPixataWithFerrets.Client;component/Images/elephant100x100.png" />

Obviously, you don’t have to add a picture of an elephant, but it won’t look as nice if you don’t! I found this one from www.findicons.com. Here’s how it looked in the Visual Studio designer...

The elephant toast in the VS designer

Notice that I’ve included a button in the XAML, and wired up the Click event. This allows the user to dismiss the toast before it times out. In this instance, the button is disguised as an elephant, which isn’t actually very user friendly. The Elephant toast style that comes with the control has a normal close button in the top right-hand corner of the toast, which is generally a better idea. People just aren’t used to the idea of clicking elephants yet!

Now, in order to get the title and message into your controls, you can override two string properties from the base class (actually from an interface that the base class implements, but let’s not split hairs)...

    public override string TitleText {
      set {
        Title.Text = value;
    public override string MessageText {
      set {
        Message.Text = value;

In this case, Title and Message are the names of the two TextBlock controls I put in the XAML. If you name yours differently, just modify this code.

The only other thing left to do is add a handler for the button’s Click event, which is used to allow the user to close the toast. Put the following code right below the two properties...

    private void Button_Click(object sender, RoutedEventArgs e) {

CloseMe is a method in the base toast class that you inherited earlier, so you don’t need to write any more code than this.

Using The Toast In Lightswitch

Now you are ready to test your control. In your Lightswitch screen’s code behind, use the second overload of the ShowToast method, that takes your new toast control instead of a value from the enumeration. Note that as you are creating a control, you need to dispatch to the main thread, to avoid access violation exceptions. When using the built-in toast styles, you don’t need to worry about this as the control does it for you...

  Dispatchers.Main.BeginInvoke(() => {
    PixataToastHelper.ShowToast("Elephants!", "Watch out for the elephants!", new MyToast(), Width: 200, Height: 100);

I have overridden the default width and height values here, and the result looks like this...

A notification toast

If you leave it alone, the toast will disappear after 4 seconds (as we didn’t override the default), or you can click the elephant and the toast will disappear immediately.

As mentioned above, it would be more intuitive to have the button look like a button, but I’ll leave that for you as an exercise. A slightly modified version of the elephant ended up in the extension.

As it stands, the control allows the display of a title and message. You can omit either of these if you wish, you just don’t override the appropriate property in your toast control’s code behind. Also, for those who find these two properties too limiting, there are ways of creating much richer, more flexible toast controls, but as this post is quite long already, I’ll leave that for another day.

I hope this is all clear, and you find the control useful. Please feel free to leave comments below.

Update 7th Mar: I have posted the follow-up article showing how to produce more advanced toasts.

Tuesday, 06 March 2012 23:11:55 (GMT Standard Time, UTC+00:00)
Love it!! Great job. This is a fantastic control.

Thanks Alan!
Monday, 19 March 2012 08:52:57 (GMT Standard Time, UTC+00:00)
HI Alan,

Can i use the toast control via the server tier?
I am new to LS and didnt figure how to do it.

Yoav Milo
Monday, 19 March 2012 14:37:43 (GMT Standard Time, UTC+00:00)
Hi Yoav,

The control isn't designed to be used from the server tier. Apart from anything else, this would mean the toast would pop up on the server, where it may not be seen (most servers don't have monitors connected, and don't have someone sitting in front of them).

What is your requirement? Maybe if you can explain that, I could help you work out the best way to sue the control.

Kol tuv
Comments are closed.