Toast Notification In Windows 10 UWP

Toast notification is a message used to display the information fast and an app can construct and deliver to the user is not currently using the app. Windows 10 toast notification have new adaptive templates and interactive actions we can set our toast layout our self, so it is also called  Adaptive Toast.

Let’s see the steps

Create a new windows 10 UWP app.

Go to code behind page and add the following namespace.

  1. using Windows.UI.Notifications;  
  2. using NotificationsExtensions.Toasts;   
Next create XML which will be used to display the Toast. Here I created Toast Generic Template like the following code:
  1. public static Windows.Data.Xml.Dom.XmlDocument CreateToast()  
  2. {  
  3.    var xDoc = new XDocument(  
  4.       new XElement("toast",  
  5.       new XElement("visual",  
  6.       new XElement("binding"new XAttribute("template""ToastGeneric"),  
  7.       new XElement("text""C# Corner"),  
  8.       new XElement("text""Do you got MVP award?")  
  9.    )  
  10.    ),// actions  
  11.    new XElement("actions",  
  12.    new XElement("action"new XAttribute("activationType""background"),  
  13.    new XAttribute("content""Yes"), new XAttribute("arguments""yes")),  
  14.    new XElement("action"new XAttribute("activationType""background"),  
  15.    new XAttribute("content""No"), new XAttribute("arguments""no"))  
  16.    )  
  17.    )  
  18.    );  
  19.   
  20.    var xmlDoc = new Windows.Data.Xml.Dom.XmlDocument();  
  21.    xmlDoc.LoadXml(xDoc.ToString());  
  22.    return xmlDoc;  
  23. }  
Create toast notification object using xml document.
  1. var xmdock = CreateToast();  
  2. var toast = new ToastNotification(xmdock);  
  3. Next show the toast using ToastNotificationManager class.  
  4. var notifi = Windows.UI.Notifications.ToastNotificationManager.CreateToastNotifier();  
  5. notifi.Show(toast);  
Full source code:
  1. private void showToastBtn_Click(object sender, RoutedEventArgs e)  
  2. {  
  3.     var xmdock = CreateToast();  
  4.     var toast = new ToastNotification(xmdock);  
  5.     var notifi = Windows.UI.Notifications.ToastNotificationManager.CreateToastNotifier();  
  6.     notifi.Show(toast);  
  7. }  
Now run the app and show the output like the following image.

Run

Source Code.

 

Next Recommended Readings