Tips and tricks for C# and JavaScript Windows 8 developers: Using notifications for non-modal messages

When you want to display a message to inform your user, it is not always a good idea to use a modal MessageDialog which can be really annoying for the user.

A better way can be to use a notification in order to display a non-intrusive dialog for the user:


To do so here is the code with C#:

public static void ShowNotification(string title, string message)
    const ToastTemplateType template = Windows.UI.Notifications.ToastTemplateType.ToastText02;
    var toastXml = Windows.UI.Notifications.ToastNotificationManager.GetTemplateContent(template);

    var toastTextElements = toastXml.GetElementsByTagName("text");

    var toast = new Windows.UI.Notifications.ToastNotification(toastXml);

    var toastNotifier = Windows.UI.Notifications.ToastNotificationManager.CreateToastNotifier();

And the JavaScript version:

var showNotification = function (title, message) {
    var notifications = Windows.UI.Notifications;
    var template = notifications.ToastTemplateType.toastText02;
    var toastXml = notifications.ToastNotificationManager.getTemplateContent(template);

    var toastTextElements = toastXml.getElementsByTagName("text");

    var toast = new notifications.ToastNotification(toastXml);

    var toastNotifier = notifications.ToastNotificationManager.createToastNotifier();;

And obviously do not forget to activate the toast capable feature in the Package.appxmanifest file:


Comments (1)

  1. Great little tid-bit here. Nice to have such a clean and to the point example of this option. I may have to implement this in my future projects. Thank you David.