Introduction
A 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 the code-behind page and add the following namespace.
- using Windows.UI.Notifications;
- using NotificationsExtensions.Toasts;
Next, create XML which will be used to display the Toast. Here I created a Toast Generic Template like the following code:
- public static Windows.Data.Xml.Dom.XmlDocument CreateToast()
- {
- var xDoc = new XDocument(
- new XElement("toast",
- new XElement("visual",
- new XElement("binding", new XAttribute("template", "ToastGeneric"),
- new XElement("text", "C# Corner"),
- new XElement("text", "Do you got MVP award?")
- )
- ),// actions
- new XElement("actions",
- new XElement("action", new XAttribute("activationType", "background"),
- new XAttribute("content", "Yes"), new XAttribute("arguments", "yes")),
- new XElement("action", new XAttribute("activationType", "background"),
- new XAttribute("content", "No"), new XAttribute("arguments", "no"))
- )
- )
- );
-
- var xmlDoc = new Windows.Data.Xml.Dom.XmlDocument();
- xmlDoc.LoadXml(xDoc.ToString());
- return xmlDoc;
- }
Create a toast notification object using XML document.
- var xmdock = CreateToast();
- var toast = new ToastNotification(xmdock);
- Next show the toast using ToastNotificationManager class.
- var notifi = Windows.UI.Notifications.ToastNotificationManager.CreateToastNotifier();
- notifi.Show(toast);
Full source code
- private void showToastBtn_Click(object sender, RoutedEventArgs e)
- {
- var xmdock = CreateToast();
- var toast = new ToastNotification(xmdock);
- var notifi = Windows.UI.Notifications.ToastNotificationManager.CreateToastNotifier();
- notifi.Show(toast);
- }
Now run the app and show the output like the following image.
Source
Code.
Summary
In this article, we learned about Toast Notification In Windows 10 UWP.