【问题标题】:windows 8 metro app - toast notificationWindows 8 Metro 应用程序 - Toast 通知
【发布时间】: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


    【解决方案1】:

    您的代码在我看来是正确的;我现在没有Win8,所以我无法测试它。但是,如果您在 VS 的“Toast Capable”字段中启用了 Toast,您可能需要检查应用程序的清单。希望这会有所帮助。

    【讨论】:

    • 这与我在自己的代码中遇到的问题相同。我没有进入应用程序的清单并设置“Toast Capable”
    【解决方案2】:

    您是否在 Package.appxmanifest 中启用了“Toast 功能”?

    【讨论】:

      【解决方案3】:

      我认为,有两个原因,

      1. 首先可能与您的应用程序的 toast 功能有关。对于您的 Package.appxmanifest 中的这组 ToastCapable="true"

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

      【讨论】:

        【解决方案4】:

        我认为你可以只使用 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";
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-04-20
          • 2014-09-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多