App.xaml.cs (2832B)
1 using System.Diagnostics; 2 using System.Windows; 3 using Windows.ApplicationModel; 4 using Windows.UI.Notifications; 5 using Windows.UI.Notifications.Management; 6 7 namespace BusynessNotification 8 { 9 10 11 /// <summary> 12 /// Interaction logic for App.xaml 13 /// </summary> 14 /// 15 16 public partial class App : System.Windows.Application 17 { 18 protected override async void OnStartup(StartupEventArgs e) 19 { 20 base.OnStartup(e); 21 ///ここにスタートアップ時の処理を記述 22 var icon = GetResourceStream(new Uri("BussynessNotificaion.ico", UriKind.Relative)).Stream; 23 var notifyIcon = new System.Windows.Forms.NotifyIcon 24 { 25 Visible = true, 26 Icon = new System.Drawing.Icon(icon), 27 Text = "BusynessNotification" 28 }; 29 notifyIcon.MouseClick += new System.Windows.Forms.MouseEventHandler(NotifyIcon_Click); 30 31 ///通知の中にBusynessNotificationからの通知が残っていれば削除する 32 ///get the listener 33 UserNotificationListener userNotificationListener = UserNotificationListener.Current; 34 ///and request access the the user's notification 35 UserNotificationListenerAccessStatus userNotificationListenerAccessStatus = await userNotificationListener.RequestAccessAsync(); 36 if(userNotificationListenerAccessStatus != UserNotificationListenerAccessStatus.Allowed) 37 { 38 39 } 40 ///get the toast notification 41 IReadOnlyList<UserNotification> notifs = await userNotificationListener.GetNotificationsAsync(NotificationKinds.Toast); 42 int i = 0; 43 44 Debug.WriteLine("length of notification is " + notifs.Count); 45 ///Debug.WriteLine("notifis is " + notifs[0]); 46 47 while (notifs.Count >= i+1) 48 { 49 Debug.WriteLine("notifiChech is called"); 50 UserNotification notification = notifs[i]; 51 Debug.WriteLine(notification.AppInfo.DisplayInfo.DisplayName); 52 if(notification.AppInfo.DisplayInfo.DisplayName == "BusynessNotification") 53 { 54 userNotificationListener.RemoveNotification(notification.Id); 55 } 56 i++; 57 } 58 59 ObserveUseage observeUseage = new(); 60 observeUseage.Controller(); 61 } 62 63 protected override void OnExit(ExitEventArgs e) 64 { 65 base.OnExit(e); 66 } 67 68 private void NotifyIcon_Click(object sender, System.Windows.Forms.MouseEventArgs e) 69 { 70 if (e.Button == System.Windows.Forms.MouseButtons.Left) 71 { 72 var wnd = new MainWindow(); 73 wnd.Show(); 74 } 75 } 76 } 77 }