Click here to Skip to main content
14,980,146 members
Articles / Desktop Programming / Universal Windows Platform
Tip/Trick
Posted 19 Sep 2016

Stats

26.9K views
789 downloads
12 bookmarked

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
A quick way to add a toast notification to uwp applications and remove when the app closes...

Introduction

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.

Background

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>" +
                      "</toast>";

  var xmlDocument = new XmlDocument();
 
  xmlDocument.LoadXml(toastTemplate);
  
  var toastNotification = new ToastNotification(xmlDocument);
 
  var notification = ToastNotificationManager.CreateToastNotifier();
            
  notification.Show(toastNotification);
}

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)
{
  removeToastHistory();
  Application.Current.Exit();
}

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;
  toastHistory.Clear();
}

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.

History

  • 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.

License

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

Share

About the Author

frankvdhorst
Netherlands Netherlands
No Biography provided

Comments and Discussions

 
QuestionGood TIP!!! Pin
B. L. Zeebub26-Sep-16 4:35
MemberB. L. Zeebub26-Sep-16 4:35 
AnswerRe: Good TIP!!! Pin
us471126-Sep-16 8:36
Memberus471126-Sep-16 8:36 
AnswerRe: Good TIP!!! Pin
frankvdhorst3-Oct-16 11:46
Memberfrankvdhorst3-Oct-16 11:46 
AnswerRe: Good TIP!!! Pin
frankvdhorst3-Oct-16 10:25
Memberfrankvdhorst3-Oct-16 10:25 
GeneralMy vote of 5 Pin
Franc Morales22-Sep-16 9:39
MemberFranc Morales22-Sep-16 9:39 
GeneralRe: My vote of 5 Pin
frankvdhorst24-Sep-16 9:54
Memberfrankvdhorst24-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
Memberfrankvdhorst22-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.