Click here to Skip to main content
15,900,907 members
Articles / Desktop Programming / Universal Windows Platform

How to Display a Toast Message and Remove It When the Application Closes

Rate me:
Please Sign up or sign in to vote.
4.07/5 (9 votes)
22 Sep 2016CPOL1 min read 36.6K   1K   12   9
A quick way to add a toast notification to uwp applications and remove when the app closes...


This is my first tip on adding a toast notification to a uwp app.

I'm not a professional programmer and don't intend to be one. Just as a hobby, I program small applications.

I have also created a very simple and very basic demonstration application. You can download the sourcecode.


For work and hobby, I change USB Comm ports a lot. You always hear a sound but never see a message like 'Serial USB device COM5 inserted'. I have created only for my personal use a simple and basic program in C# for UWP. This program listens for USB insertions en checks if it is a serial port. Then when I insert a USB Comm port device I see a toast message with the text "device COM5 inserted". For my very handy.

Using the Code

To display a toast, you need to set up a simple template.

//Display a toast message
private void displayToastNotification(String caption, String message)
  var toastTemplate = "<toast launch=\"app-defined-string\">" +
                      "<visual>" +
                        "<binding template =\"ToastGeneric\">" +
                          "<text>" + caption + "</text>" +
                          "<text>" + message + "</text>" +
                        "</binding>" +
                      "</visual>" +

  var xmlDocument = new XmlDocument();
  var toastNotification = new ToastNotification(xmlDocument);
  var notification = ToastNotificationManager.CreateToastNotifier();

When the user clicks the button 'Toast' a toast message appears.

//Button Toast
private void btnToasTest_Click(object sender, RoutedEventArgs e)
  displayToastNotification("Hello world!", "This is my message!");

When the user clicks the button 'Close' all toast messages are remove from notification center

 //Button Close
private void btnCloseApp_Click(object sender, RoutedEventArgs e)

When the user closes the application, I want to clear the notifications too. I accomplished this with this simple function.

//Remove toast history
private void removeToastHistory()
  var toastHistory = ToastNotificationManager.History;

That's all....

Points of Interest

On the internet, I found a lot of examples how to create a toast notification but not how to neatly remove it when the application closes. I solved it like this.


  • No history yet..... I might post my simple USB COMM port watcher application in the future. For now only the very simple and very basic application for the toast only.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Netherlands Netherlands
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

QuestionGood TIP!!! Pin
B. L. Zeebub26-Sep-16 4:35
B. L. Zeebub26-Sep-16 4:35 
AnswerRe: Good TIP!!! Pin
us471126-Sep-16 8:36
us471126-Sep-16 8:36 
AnswerRe: Good TIP!!! Pin
frankvdhorst3-Oct-16 11:46
frankvdhorst3-Oct-16 11:46 
AnswerRe: Good TIP!!! Pin
frankvdhorst3-Oct-16 10:25
frankvdhorst3-Oct-16 10:25 
GeneralMy vote of 5 Pin
Franc Morales22-Sep-16 9:39
Franc Morales22-Sep-16 9:39 
GeneralRe: My vote of 5 Pin
frankvdhorst24-Sep-16 9:54
frankvdhorst24-Sep-16 9:54 
Question1 Pin
Kevin Marois20-Sep-16 11:34
professionalKevin Marois20-Sep-16 11:34 
AnswerRe: 1 Pin
frankvdhorst22-Sep-16 8:46
frankvdhorst22-Sep-16 8:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.