Karen
Karen

Reputation: 31

windows 8 metro app - toast notification

I am developing a Windows 8 metro-style application using toast notification. (C# + xaml combination) I looked into MS metro style sample code and tried to apply it to my project, looks like I used the code exactly the same way, but I don't know why it is not working..

(There is no error, it builds successfully but just doesn't work.)

What I'm trying to do is very simple:

There is a button. When the button_click event occurs, I'd like to pop a toast notification.

This is what I did:

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();
        }

    }
}

If I click the button, nothing happens. Why?

Upvotes: 3

Views: 2902

Answers (4)

Hoby
Hoby

Reputation: 1077

i think you can just use Xml String

        // 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";

Upvotes: 0

N K
N K

Reputation: 3327

I think, there are two reasons,

  1. First may be relating to toast capability of your application. For this set ToastCapable="true" in your Package.appxmanifest

  2. Second one is run application in Local Machine rather than Simulator. I found that Simulator is not able to produce Toast notification.

Upvotes: 1

Balint Bako
Balint Bako

Reputation: 2560

Did you enable "Toast capable" in Package.appxmanifest?

Upvotes: 3

XiaoChuan Yu
XiaoChuan Yu

Reputation: 4011

Your code looks correct to me; I don't have Win8 with me here right now so I can't test it. However, you may want to check your app's manifest if you enabled Toast or not in the "Toast Capable" field in VS. Hope this helps.

Upvotes: 3

Related Questions