How to create informative toast notification in UWP App

Zea Shah picture Zea Shah · May 31, 2016 · Viewed 11.6k times · Source

In my app, I want to inform user when particular action had performed, like record updated successfully or new record added, but there's not inbuilt control to that can display such information. Is there anything similar to Android Toast.makeText for UWP?

Answer

Daniel Krzyczkowski picture Daniel Krzyczkowski · May 31, 2016

Yes, UWP has Toast Notifications :)

Here is sample code to display simple notification:

private void ShowToastNotification(string title, string stringContent)
{
        ToastNotifier ToastNotifier = ToastNotificationManager.CreateToastNotifier();
        Windows.Data.Xml.Dom.XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText02);
        Windows.Data.Xml.Dom.XmlNodeList toastNodeList = toastXml.GetElementsByTagName("text");
        toastNodeList.Item(0).AppendChild(toastXml.CreateTextNode(title));
        toastNodeList.Item(1).AppendChild(toastXml.CreateTextNode(stringContent));
        Windows.Data.Xml.Dom.IXmlNode toastNode = toastXml.SelectSingleNode("/toast");
        Windows.Data.Xml.Dom.XmlElement audio = toastXml.CreateElement("audio");
        audio.SetAttribute("src", "ms-winsoundevent:Notification.SMS");

        ToastNotification toast = new ToastNotification(toastXml);
        toast.ExpirationTime = DateTime.Now.AddSeconds(4);
        ToastNotifier.Show(toast);
}

In this article you can find how to customize it:

https://docs.microsoft.com/en-us/windows/uwp/controls-and-patterns/tiles-and-notifications-adaptive-interactive-toasts