Advertisement
Guest User

Untitled

a guest
May 21st, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.14 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Windows;
  4. using System.Runtime;
  5. using Windows.UI.Notifications;
  6. using Windows.Data.Xml.Dom;
  7.  
  8. namespace DesktopToastsSample
  9. {
  10.     public partial class MainWindow : Window
  11.     {
  12.         private const String APP_ID = "Microsoft.Samples.DesktopToastsSample";
  13.         public MainWindow()
  14.         {
  15.             InitializeComponent();
  16.             ShowToastButton.Click += ShowToastButton_Click;
  17.         }
  18.  
  19.         // Create and show the toast.
  20.         // See the "Toasts" sample for more detail on what can be done with toasts
  21.         private void ShowToastButton_Click(object sender, RoutedEventArgs e)
  22.         {
  23.  
  24.             // Get a toast XML template
  25.             XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastImageAndText04);
  26.  
  27.             // Fill in the text elements
  28.             XmlNodeList stringElements = toastXml.GetElementsByTagName("text");
  29.             for (int i = 0; i < stringElements.Length; i++)
  30.             {
  31.                 stringElements[i].AppendChild(toastXml.CreateTextNode("Line " + i));
  32.             }
  33.  
  34.             // Specify the absolute path to an image
  35.             String imagePath = "file:///" + Path.GetFullPath("toastImageAndText.png");
  36.             XmlNodeList imageElements = toastXml.GetElementsByTagName("image");
  37.             imageElements[0].Attributes.GetNamedItem("src").NodeValue = imagePath;
  38.  
  39.             // Create the toast and attach event listeners
  40.             ToastNotification toast = new ToastNotification(toastXml);
  41.             toast.Activated += ToastActivated;
  42.             toast.Dismissed += ToastDismissed;
  43.             toast.Failed += ToastFailed;
  44.  
  45.             // Show the toast. Be sure to specify the AppUserModelId on your application's shortcut!
  46.             ToastNotificationManager.CreateToastNotifier(APP_ID).Show(toast);
  47.  
  48.         }
  49.  
  50.         private void ToastActivated(ToastNotification sender, object e)
  51.         {
  52.             Dispatcher.Invoke(() =>
  53.             {
  54.                 Activate();
  55.                 Output.Text = "The user activated the toast.";
  56.             });
  57.         }
  58.  
  59.         private void ToastDismissed(ToastNotification sender, ToastDismissedEventArgs e)
  60.         {
  61.             String outputText = "";
  62.             switch (e.Reason)
  63.             {
  64.                 case ToastDismissalReason.ApplicationHidden:
  65.                     outputText = "The app hid the toast using ToastNotifier.Hide";
  66.                     break;
  67.                 case ToastDismissalReason.UserCanceled:
  68.                     outputText = "The user dismissed the toast";
  69.                     break;
  70.                 case ToastDismissalReason.TimedOut:
  71.                     outputText = "The toast has timed out";
  72.                     break;
  73.             }
  74.  
  75.             Dispatcher.Invoke(() =>
  76.             {
  77.                 Output.Text = outputText;
  78.             });
  79.         }
  80.  
  81.         private void ToastFailed(ToastNotification sender, ToastFailedEventArgs e)
  82.         {
  83.             Dispatcher.Invoke(() =>
  84.             {
  85.                 Output.Text = "The toast encountered an error.";
  86.             });
  87.         }
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement