Advertisement
elGuille

Código de C# del ejemplo de toast notifications

Jan 17th, 2013
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.96 KB | None | 0 0
  1. //-----------------------------------------------------------------------------
  2. // Ejemplo de toast notifications                                   (18/Ene/13)
  3. //
  4. // El artículo en mi blog:
  5. // http://www.elguillemola.com/index.php/2013/01/ejemplo-sencillo-de-notificaciones-toast-para-windows-store/
  6. //
  7. // ©Guillermo 'guille' Som, 2013
  8. //-----------------------------------------------------------------------------
  9.  
  10. using System;
  11. using System.Collections.Generic;
  12. using System.IO;
  13. using System.Linq;
  14. using Windows.Foundation;
  15. using Windows.Foundation.Collections;
  16. using Windows.UI.Xaml;
  17. using Windows.UI.Xaml.Controls;
  18. using Windows.UI.Xaml.Controls.Primitives;
  19. using Windows.UI.Xaml.Data;
  20. using Windows.UI.Xaml.Input;
  21. using Windows.UI.Xaml.Media;
  22. using Windows.UI.Xaml.Navigation;
  23.  
  24. using Windows.UI.Notifications;
  25. using Windows.Data.Xml.Dom;
  26.  
  27. // The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238
  28.  
  29. namespace Toast_notifications_CS
  30. {
  31.     /// <summary>
  32.     /// An empty page that can be used on its own or navigated to within a Frame.
  33.     /// </summary>
  34.     public sealed partial class MainPage : Page
  35.     {
  36.         public MainPage()
  37.         {
  38.             this.InitializeComponent();
  39.         }
  40.  
  41.         /// <summary>
  42.         /// Invoked when this page is about to be displayed in a Frame.
  43.         /// </summary>
  44.         /// <param name="e">Event data that describes how this page was reached.  The Parameter
  45.         /// property is typically used to configure the page.</param>
  46.         protected override void OnNavigatedTo(NavigationEventArgs e)
  47.         {
  48.         }
  49.  
  50.         private void ButtonPlay_Click(object sender, RoutedEventArgs e)
  51.         {
  52.             // La plantilla a usar, esta Text03 es:
  53.             // un texto de cabecera que puede ocupar dos líneas y una línea de texto normal
  54.             ToastTemplateType toastTemplate = ToastTemplateType.ToastText03;
  55.  
  56.             // Asignamos el template a un documento Xml
  57.             XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(toastTemplate);
  58.  
  59.             // El texto para el primer elemento de la pantilla
  60.             XmlNodeList toastTextElements = toastXml.GetElementsByTagName("text");
  61.             toastTextElements[0].AppendChild(toastXml.CreateTextNode("Cargando ..."));
  62.  
  63.             // Si queremos que la duración sea larga
  64.             // Puede ser corta o larga, corta es la predeterminada
  65.             IXmlNode toastNode = toastXml.SelectSingleNode("/toast");
  66.             ((XmlElement)toastNode).SetAttribute("duration", "long");
  67.  
  68.             // Si queremos quitar el sonido
  69.             // (o indicar alguno en particular)
  70.             // tenemos que usar el elemento "audio"
  71.  
  72.             // IXmlNode toastNode = toastXml.SelectSingleNode("/toast");
  73.             XmlElement audio = toastXml.CreateElement("audio");
  74.             audio.SetAttribute("silent", "true");
  75.             toastNode.AppendChild(audio);
  76.  
  77.             ToastNotification toast = new ToastNotification(toastXml);
  78.             ToastNotificationManager.CreateToastNotifier().Show(toast);
  79.         }
  80.  
  81.         private void ButtonClock_Click(object sender, RoutedEventArgs e)
  82.         {
  83.             // La plantilla a usar, esta Text03 es:
  84.             // un texto de cabecera que puede ocupar dos líneas y una línea de texto normal
  85.             ToastTemplateType toastTemplate = ToastTemplateType.ToastText03;
  86.  
  87.             // Asignamos el template a un documento Xml
  88.             XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(toastTemplate);
  89.  
  90.             // El texto para el primer elemento de la pantilla
  91.             XmlNodeList toastTextElements = toastXml.GetElementsByTagName("text");
  92.             toastTextElements[0].AppendChild(
  93.                 toastXml.CreateTextNode("A los 3 segundos después de haber pulsado en el botón."));
  94.             toastTextElements[1].AppendChild(
  95.                 toastXml.CreateTextNode("Segundo texto."));
  96.  
  97.             // Esto es para indicar que esta notificación se hará en el momento indicado
  98.             DateTime dueTime = DateTime.Now.AddSeconds(3);
  99.             ScheduledToastNotification scheduledToast = new ScheduledToastNotification(toastXml, dueTime);
  100.  
  101.             ToastNotificationManager.CreateToastNotifier().AddToSchedule(scheduledToast);
  102.         }
  103.  
  104.         private void ButtonHelp_Click(object sender, RoutedEventArgs e)
  105.         {
  106.             // La plantilla a usar, esta Text02 es:
  107.             // un texto de cabecera y un texto normal que puede ocupar dos líneas
  108.             ToastTemplateType toastTemplate = ToastTemplateType.ToastText02;
  109.             // Asignamos el template a un documento Xml
  110.             XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(toastTemplate);
  111.  
  112.             // El texto para el primero elemento de la pantilla
  113.             XmlNodeList toastTextElements = toastXml.GetElementsByTagName("text");
  114.             toastTextElements[0].AppendChild(
  115.                 toastXml.CreateTextNode("Esto se mostrará durante más tiempo ..."));
  116.             toastTextElements[1].AppendChild(
  117.                 toastXml.CreateTextNode("Siempre puedes cerrar las notificaciones en la X superior."));
  118.  
  119.             // Si queremos que la duración sea larga
  120.             // Puede ser corta o larga, corta es la predeterminada
  121.             IXmlNode toastNode = toastXml.SelectSingleNode("/toast");
  122.             (toastNode as XmlElement).SetAttribute("duration", "long");
  123.  
  124.             // Esto es para indicar que esta notificación se hará en el momento indicado
  125.             DateTime dueTime = DateTime.Now.AddSeconds(10);
  126.             ScheduledToastNotification scheduledToast = new ScheduledToastNotification(toastXml, dueTime);
  127.  
  128.             ToastNotificationManager.CreateToastNotifier().AddToSchedule(scheduledToast);
  129.  
  130.             ToastNotification toast = new ToastNotification(toastXml);
  131.             ToastNotificationManager.CreateToastNotifier().Show(toast);
  132.         }
  133.     }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement