Windows Phone 8.1 Toast通知

network | | 访问(61)
<StackPanel>
    <Button Content="ToastText01模板通知" x:Name="toastText01" Click="toastText01_Click" Width="370"></Button>
    <Button Content="ToastText02模板通知" x:Name="toastText02" Click="toastText02_Click" Width="370"></Button>
    <Button Content="XML模板通知" x:Name="toastXML" Click="toastXML_Click" Width="370"></Button>
    <Button Content="定时通知" x:Name="scheduledToast" Click="scheduledToast_Click" Width="370"></Button>
    <TextBlock x:Name="info"></TextBlock>
</StackPanel>
public sealed partial class MainPage: Page {
    public MainPage() {
        this.InitializeComponent();

        this.NavigationCacheMode = NavigationCacheMode.Required;
    }

    private void toastText01_Click(object sender, RoutedEventArgs e) {
        XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText01);
        XmlNodeList elements = toastXml.GetElementsByTagName("text");
        elements[0].AppendChild(toastXml.CreateTextNode("Hello Windows Phone 8.1"));
        ToastNotification toast = new ToastNotification(toastXml);
        toast.Activated += toast_Activated;
        toast.Dismissed += toast_Dismissed;
        toast.Failed += toast_Failed;
        ToastNotificationManager.CreateToastNotifier().Show(toast);
    }

    private void toastText02_Click(object sender, RoutedEventArgs e) {
        XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText02);
        XmlNodeList elements = toastXml.GetElementsByTagName("text");
        elements[0].AppendChild(toastXml.CreateTextNode("WP8.1"));
        elements[1].AppendChild(toastXml.CreateTextNode("Hello Windows Phone 8.1"));
        ToastNotification toast = new ToastNotification(toastXml);
        toast.Activated += toast_Activated;
        toast.Dismissed += toast_Dismissed;
        toast.Failed += toast_Failed;
        ToastNotificationManager.CreateToastNotifier().Show(toast);
    }

    private void toastXML_Click(object sender, RoutedEventArgs e) {
        string toastXmlString = "<toast>" + "<visual>" + "<binding template='ToastText02'>" + 
        "<text id='1'>WP8.1</text>" + "<text id='2'>" + "Received: " + 
        DateTime.Now.ToLocalTime() + "</text>" + "</binding>" + "</visual>" + "</toast>";

        XmlDocument toastXml = new XmlDocument();
        toastXml.LoadXml(toastXmlString);
        ToastNotification toast = new ToastNotification(toastXml);
        toast.Activated += toast_Activated;
        toast.Dismissed += toast_Dismissed;
        toast.Failed += toast_Failed;
        ToastNotificationManager.CreateToastNotifier().Show(toast);
    }

    async void toast_Failed(ToastNotification sender, ToastFailedEventArgs args) {
        await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () = >{
            info.Text = "Toast通知失败:" + args.ErrorCode.Message;
        });
    }

    async void toast_Dismissed(ToastNotification sender, ToastDismissedEventArgs args) {
        await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () = >{
            info.Text = "Toast通知消失:" + args.Reason.ToString();
        });
    }

    async void toast_Activated(ToastNotification sender, object args) {
        await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () = >{
            info.Text = "Toast通知激活";
        });
    }

    private void scheduledToast_Click(object sender, RoutedEventArgs e) {
        XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText02);
        XmlNodeList toastNodeList = toastXml.GetElementsByTagName("text");
        toastNodeList.Item(0).AppendChild(toastXml.CreateTextNode("Toast title"));
        toastNodeList.Item(1).AppendChild(toastXml.CreateTextNode("Toast content"));
        DateTime startTime = DateTime.Now.AddSeconds(3);
        ScheduledToastNotification recurringToast = new ScheduledToastNotification(toastXml, startTime);
        recurringToast.Id = "ScheduledToast1";
        ToastNotificationManager.CreateToastNotifier().AddToSchedule(recurringToast);
    }
}