【发布时间】:2012-02-24 00:29:10
【问题描述】:
我正在使用 toast 通知开发一个 Windows 8 Metro 风格的应用程序。 (C# + xaml 组合) 我查看了 MS Metro 风格的示例代码并尝试将其应用到我的项目中, 看起来我使用代码的方式完全相同,但我不知道为什么它不起作用..
(没有错误,它构建成功但不起作用。)
我想做的很简单:
有一个按钮。
当button_click 事件发生时,我想弹出一个 toast 通知。
这就是我所做的:
namespace Application1
{
public sealed partial class BlankPage : Page
{
public BlankPage()
{
this.InitializeComponent();
Scenario2Init();
}
void Scenario2Init()
{
toastTest.Click += (sender, e) => { ToastAlarm(true); };
}
void ToastAlarm(bool loopAudio)
{
XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText02);
// Toasts can optionally be set to long duration by adding the 'duration' attribute
IXmlNode toastNode = toastXml.SelectSingleNode("/toast");
((XmlElement)toastNode).SetAttribute("duration", "long");
// This XmlNodeList will have two items since the template we are using has two text fields.
XmlNodeList stringElements = toastXml.GetElementsByTagName("text");
stringElements.Item(0).AppendChild(toastXml.CreateTextNode("Long Duration Toast"));
XmlElement audioElement = toastXml.CreateElement("audio");
if (loopAudio)
{
// Long-duration Toasts can optionally loop audio using the 'loop' attribute
audioElement.SetAttribute("src", "ms-winsoundevent:Notification.Looping.Alarm");
audioElement.SetAttribute("loop", "true");
stringElements.Item(1).AppendChild(toastXml.CreateTextNode("Looping audio"));
}
else
{
audioElement.SetAttribute("src", "ms-winsoundevent:Notification.IM");
}
toastNode.AppendChild(audioElement);
ToastNotification toast = new ToastNotification(toastXml);
ToastNotificationManager.CreateToastNotifier().Show(toast);
//Scenario2OutputText.Text = toastXml.GetXml();
}
}
}
如果我点击按钮,什么都不会发生。为什么?
【问题讨论】:
标签: notifications toast microsoft-metro