When building a Windows RT application (based on C#) it is pretty common that you will want to notify your user of an event. It is more common that you are going to want to do this in a way which is not intrusive, aka does not get in the way of the user. Luckily for you the WinRT team has built a great toast notification engine into the framework.
In fact this notification engine allows for toasts to be both in-application as well as remote notifications. It also allows for notifications to be real time (sent when at the time you create them) as well as scheduling them to be sent at some predetermined date/time in the future.
In this post I am only going to show how to do in-application toasts, I will focus on cloud base toasts in a future posts. Before we dive into the code, take a second to look at the Toast Template Catalog for all the various ways we can layout/format the toast.
Getting started.
What does a toast look like?
How do create toasts?
- You must enable toasts via the Package.appxmanifest
If you do not enable toasts you will NOT get a toasts, nor will you get an error, it will simply NOT WORK.
- Check to see if toasts have been enabled
var notifier = ToastNotificationManager.CreateToastNotifier();
if (notifier.Setting != NotificationSetting.Enabled)
{
// do something here
}
In the above we are first creating the notifier, then we simply check the Setting property. If it is not enabled you may want to punt or alert the user.
- Create the code to create the toasts – Real time toasts
var notifier = ToastNotificationManager.CreateToastNotifier();
var template = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText02);
var element = template.GetElementsByTagName("text")[0];
element.AppendChild(template.CreateTextNode("Line 1 goes here"));
var toast = new ToastNotification(template);
notifier.Show(toast);
If you look at the code above one thing should jump out at you right way, the fact you have to drop down to raw XML. Yea, it sucks. The toast team decided to NOT wrap this in the same way as the Tile team, but oh well.
In the above we are first creating the notifier, then we will chose the template we want. Once we do that we just start updating the values as expected.
- Create to code to create scheduled toasts
var notifier = ToastNotificationManager.CreateToastNotifier();
var template = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText02);
var element = template.GetElementsByTagName("text")[0];
element.AppendChild(template.CreateTextNode("Line 1 goes here"));
// Schedule the toast to appear 10 seconds from now
var date = DateTimeOffset.Now.AddSeconds(10);
var scheduledToast = new ScheduledToastNotification(template, date);
notifier.AddToSchedule(scheduledToast);
If you look at the code above about the only thing that has changed is that we are now creating a ‘ScheduledToastNotification’ instance and adding this to the ToastNotifiier.
As you can see doing in-application toast notifications is very easy and only requires a few lines of code.
Till next time,
Posted
11-14-2012 5:52 PM
by
Derik Whittaker