public class ToastMessage { public static void Toast(string msg) { //1. create element ToastTemplateType toastTemplate = ToastTemplateType.ToastImageAndText01; XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(toastTemplate); //2.设置消息文本 XmlNodeList toastTextElements = toastXml.GetElementsByTagName("text"); toastTextElements[0].AppendChild(toastXml.CreateTextNode(msg)); //3. 图标 XmlNodeList toastImageAttributes = toastXml.GetElementsByTagName("image"); ((XmlElement)toastImageAttributes[0]).SetAttribute("src", $"ms-appx:///assets/demo.png"); ((XmlElement)toastImageAttributes[0]).SetAttribute("alt", "logo"); // 4. duration IXmlNode toastNode = toastXml.SelectSingleNode("/toast"); ((XmlElement)toastNode).SetAttribute("duration", "short"); // 5. audio XmlElement audio = toastXml.CreateElement("audio"); audio.SetAttribute("src", $"ms-winsoundevent:Notification.SMS"); toastNode.AppendChild(audio); ToastNotification toast = new ToastNotification(toastXml); ToastNotificationManager.CreateToastNotifier().Show(toast); } }
静态函数调用,如方法:ToastMessage.Toast(Content);