个人资料

跳过导航链接首页 > 博客列表 > 博客正文

UWP弹出本地磁贴通知

分类:

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);

songshizhao
最初发表2017/12/3 21:19:31 最近更新2017/12/3 21:19:31 2298
为此篇作品打分
10
   评论