我如何以编程方式将音频添加到Toast通知中?
作者:互联网
我有如下代码:
ToastNotifier ToastNotifier = ToastNotificationManager.CreateToastNotifier();
XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText02);
XmlNodeList toastNodeList = toastXml.GetElementsByTagName("text");
toastNodeList.Item(0).AppendChild(toastXml.CreateTextNode(title));
toastNodeList.Item(1).AppendChild(toastXml.CreateTextNode(stringContent));
XmlElement audio = toastXml.CreateElement("audio");
audio.SetAttribute("src", "ms-winsoundevent:Notification.Alarm7");
audio.SetAttribute("loop", "false");
但是,使用以下方法进行测试:
toastXml.GetXml();
显示音频未添加到xml.我该如何添加它.音频的文档是here.
解决方法:
如果您使用的是Windows 10,我们强烈建议您使用通知库.见official documentation showing how to add audio敬酒.
使用Notifications库,您的代码看起来更加简洁
ToastContent toastContent = new ToastContent()
{
Visual = new ToastVisual()
{
},
Audio = new ToastAudio()
{
Src = new Uri("ms-appx:///Assets/Audio/CustomToastAudio.m4a")
};
};
但是,如果您喜欢手动使用XML DOM,或者必须将Windows 8作为目标,则错误是您从未添加音频元素.请参阅下面的最后一行.
XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText02);
...
XmlElement audio = toastXml.CreateElement("audio");
audio.SetAttribute("src", "ms-winsoundevent:Notification.Alarm7");
audio.SetAttribute("loop", "false");
// Add the audio element
toastXml.DocumentElement.AppendChild(audio);
标签:uwp,win-universal-app,windows-store-apps,c 来源: https://codeday.me/bug/20191109/2012493.html