且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用C#在UWP中发送Toast消息时出现问题

更新时间:2022-06-19 07:04:24

您可以创建一个通用函数-

you can create a common function -

using Windows.UI.Notifications;

public static 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);
}