1
+ using System . Collections . Generic ;
2
+ using System . Collections . ObjectModel ;
3
+ using System . Diagnostics ;
4
+ using Avalonia ;
5
+ using Avalonia . Controls ;
6
+ using Avalonia . Interactivity ;
7
+ using Avalonia . Markup . Xaml ;
8
+ using DesktopNotifications ;
9
+
10
+ namespace Example . Avalonia
11
+ {
12
+ public class MainWindow : Window
13
+ {
14
+ private readonly TextBox _bodyTextBox ;
15
+ private readonly ListBox _eventsListBox ;
16
+ private readonly TextBox _titleTextBox ;
17
+ private readonly INotificationManager _notificationManager ;
18
+
19
+ public MainWindow ( )
20
+ {
21
+ InitializeComponent ( ) ;
22
+ #if DEBUG
23
+ this . AttachDevTools ( ) ;
24
+ #endif
25
+
26
+ _titleTextBox = this . FindControl < TextBox > ( "TitleTextBox" ) ;
27
+ _bodyTextBox = this . FindControl < TextBox > ( "BodyTextBox" ) ;
28
+ _eventsListBox = this . FindControl < ListBox > ( "EventsListBox" ) ;
29
+ _eventsListBox . Items = new ObservableCollection < string > ( ) ;
30
+
31
+ _notificationManager = AvaloniaLocator . Current . GetService < INotificationManager > ( ) ;
32
+ _notificationManager . NotificationActivated += OnNotificationActivated ;
33
+ _notificationManager . NotificationDismissed += OnNotificationDismissed ;
34
+ }
35
+
36
+ private void OnNotificationDismissed ( object ? sender , NotificationDismissedEventArgs e )
37
+ {
38
+ ( ( IList < string > ) _eventsListBox . Items ) . Add ( $ "Notification dismissed: { e . Reason } ") ;
39
+ }
40
+
41
+ private void OnNotificationActivated ( object ? sender , NotificationActivatedEventArgs e )
42
+ {
43
+ ( ( IList < string > ) _eventsListBox . Items ) . Add ( $ "Notification activated: { e . ActionId } ") ;
44
+ }
45
+
46
+ private void InitializeComponent ( )
47
+ {
48
+ AvaloniaXamlLoader . Load ( this ) ;
49
+ }
50
+
51
+ public void Button_OnClick ( object ? sender , RoutedEventArgs e )
52
+ {
53
+ Debug . Assert ( _notificationManager != null ) ;
54
+
55
+ _notificationManager . ShowNotification ( new Notification
56
+ {
57
+ Title = _titleTextBox . Text ?? _titleTextBox . Watermark ,
58
+ Body = _bodyTextBox . Text ?? _bodyTextBox . Watermark
59
+ } ) ;
60
+ }
61
+ }
62
+ }
0 commit comments