Monday, 1 September 2014

Toast Notification

Toast Notifications are those little rectangles in the top right corner that inform the user of something, whether it's that you've got mail or simply a replacement for the dreaded successful feedback message box. So let's create a simple Toast Notification add a button named ToastButton to your form and give it a click event handler

void ToastButton_Click(object sender, RoutedEventArgs e)
{
    //Initialize the toast xml, using a predefiend type.
    var ToastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText01);

    //extract all the text elements
    var toastTextElements = ToastXml.GetElementsByTagName("text");

    //set the one text element we have to a value
    toastTextElements[0].AppendChild(ToastXml.CreateTextNode("Woot Woot!!!"));

    //create the toast
    var Toast = new ToastNotification(ToastXml);

    //show the toast in the top right corner
    ToastNotificationManager.CreateToastNotifier().Show(Toast);

}

now when you click the ToastButton a little notification will appear in the top right corner containing our text.

Now we can actually add a Failed, Dismissed and activated handlers to our toast for some richer interaction

//create the toast
var Toast = new ToastNotification(ToastXml);

Toast.Activated += Toast_Activated;

and then create an event handler to do some stuff

void Toast_Activated(ToastNotification sender, object args)
{
    //Do some stuff
}

in short that's about it, pretty straight forward.