我正在开发一个使用toast通知的Windows 8 Metro样式应用程序(C#+ xaml组合)。我查看了微软Metro样式示例代码,并尝试将其应用于我的项目,看起来我以完全相同的方式使用了代码,但我不知道为什么它不起作用。(没有错误信息,编译成功但是无法运行。)
我要做的事情非常简单:有一个按钮。当“button_click”事件发生时,我想弹出一个toast通知。
这就是我所做的:
如果我点击按钮,什么都不会发生。为什么?
我要做的事情非常简单:有一个按钮。当“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();
}
}
}
如果我点击按钮,什么都不会发生。为什么?