Advertisement
wheelsmanx

C# Toast Notification Basic

Apr 27th, 2017
388
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. /*
  2. I used this thread to help make this code - I am sharing my success.
  3.  
  4. *warning I am kind of new to C# so my code probably sucks- but it does work and is pretty simplistic and that's more than I can say for most solutions I have found*
  5.  
  6. Also I was having a hell of a time getting the xml document to read. I was fighting with System.xml (I think) and Windows.Data.Dom.Xml (also not completely sure).
  7. In the end I settled on making them hard coded strings for my example file and used a switch statement to switch between them.
  8. I have found a ton of people, looking for the solution that I have come up with, on stack overflow. It seems use of the toast notification system with console or background applications would be super useful, and the documentation that surrounds the toast notification system with windows applications all suggest that it needs to be used with an application. The Action Center is super useful for notifications vrs the NotificationTray/NotifyIcon route. I have not found a full solution anywhere else on the web. Here is example code.
  9. */
  10.  
  11. /*
  12. At first you need to declare that your program will be using winRT libraries:
  13. 1. Right click on your yourProject, select Unload Project
  14. 2. Right click on your youProject(unavailable) and click Edit yourProject.csproj
  15. 3. Add a new property group:<TargetPlatformVersion>8.0</TargetPlatformVersion>
  16. 4. Reload project
  17. 5. Add referece Windows from Windows > Core
  18. */
  19. using System;
  20. using Windows.Data.Xml.Dom;
  21. using Windows.Storage;
  22. using Windows.Storage.Streams;
  23. using System.Collections.Generic;
  24. using System.Linq;
  25. using System.Text;
  26. using System.Threading.Tasks;
  27. using Windows.UI.Notifications;
  28.  
  29. namespace ConsoleApplication6
  30. {
  31. public class NewToastNotification
  32. {
  33. public NewToastNotification(string input, int type)
  34. {
  35. string NotificationTextThing = input;
  36. string Toast = "";
  37. switch (type)
  38. {
  39. case 1:
  40. {
  41. //Basic Toast
  42. Toast = "<toast><visual><binding template=\"ToastImageAndText01\"><text id = \"1\" >";
  43. Toast += NotificationTextThing;
  44. Toast += "</text></binding></visual></toast>";
  45. break;
  46. }
  47. default:
  48. {
  49. Toast = "<toast><visual><binding template=\"ToastImageAndText01\"><text id = \"1\" >";
  50. Toast += "Default Text String";
  51. Toast += "</text></binding></visual></toast>";
  52. break;
  53. }
  54. }
  55. XmlDocument tileXml = new XmlDocument();
  56. tileXml.LoadXml(Toast);
  57. var toast = new ToastNotification(tileXml);
  58. ToastNotificationManager.CreateToastNotifier("New Toast Thing").Show(toast);
  59. }
  60. }
  61.  
  62. class Program
  63. {
  64. static void Main(string[] args)
  65. {
  66. NewToastNotification Window = new NewToastNotification("Yes",1);
  67.  
  68.  
  69. }
  70. }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement