Windows 8 Metro 应用 - 弹出式通知

3
我正在开发一个使用toast通知的Windows 8 Metro样式应用程序(C#+ xaml组合)。我查看了微软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();
        }

    }
}

如果我点击按钮,什么都不会发生。为什么?
4个回答

3
你在Package.appxmanifest中启用了“Toast capable”吗?

3

我认为你的代码是正确的,但我现在没有Win8,所以无法进行测试。然而,你可能需要检查一下你的应用程序清单,看看在VS中是否启用了Toast功能。希望这能帮到你。


这是我在自己的代码中遇到的相同问题。我没有进入应用程序清单并设置“Toast Capable”。 - David Burela

1

我认为有两个原因,

  1. 第一个可能与您的应用程序的Toast功能有关。为此,请在Package.appxmanifest中设置 ToastCapable="true"

  2. 第二个是在本地计算机上运行应用程序,而不是模拟器。我发现模拟器无法产生Toast通知。


0

我认为你可以直接使用XML字符串

        // Create the toast content by direct string manipulation.
        // See the Toasts SDK Sample for other ways of generating toasts.
        string toastXmlString =
            "<toast duration=\"long\">\n" +
                "<visual>\n" +
                    "<binding template=\"ToastText02\">\n" +
                        "<text id=\"1\">Alarms Notifications SDK Sample App</text>\n" +
                        "<text id=\"2\">" + alarmName + "</text>\n" +
                    "</binding>\n" +
                "</visual>\n" +
                "<commands scenario=\"alarm\">\n" +
                    "<command id=\"snooze\"/>\n" +
                    "<command id=\"dismiss\"/>\n" +
                "</commands>\n" +
                "<audio src=\"ms-winsoundevent:Notification.Looping.Alarm5\" loop=\"true\" />\n" +
            "</toast>\n";

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接