How to create informative toast notification in UWP App

前端 未结 3 773
野趣味
野趣味 2021-02-08 11:15

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

3条回答
  •  深忆病人
    2021-02-08 11:43

    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

提交回复
热议问题