Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create informative toast notification in UWP App

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?

like image 283
Zea Shah Avatar asked May 31 '16 09:05

Zea Shah


1 Answers

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://learn.microsoft.com/en-us/windows/uwp/controls-and-patterns/tiles-and-notifications-adaptive-interactive-toasts

like image 113
Daniel Krzyczkowski Avatar answered Oct 13 '22 09:10

Daniel Krzyczkowski