MainWindowViewModel.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using System.Reactive;
  2. using Avalonia.Controls.ApplicationLifetimes;
  3. using Avalonia.Controls.Notifications;
  4. using Avalonia.Dialogs;
  5. using ReactiveUI;
  6. namespace ControlCatalog.ViewModels
  7. {
  8. class MainWindowViewModel : ReactiveObject
  9. {
  10. private IManagedNotificationManager _notificationManager;
  11. public MainWindowViewModel(IManagedNotificationManager notificationManager)
  12. {
  13. _notificationManager = notificationManager;
  14. ShowCustomManagedNotificationCommand = ReactiveCommand.Create(() =>
  15. {
  16. NotificationManager.Show(new NotificationViewModel(NotificationManager) { Title = "Hey There!", Message = "Did you know that Avalonia now supports Custom In-Window Notifications?" });
  17. });
  18. ShowManagedNotificationCommand = ReactiveCommand.Create(() =>
  19. {
  20. NotificationManager.Show(new Avalonia.Controls.Notifications.Notification("Welcome", "Avalonia now supports Notifications.", NotificationType.Information));
  21. });
  22. ShowNativeNotificationCommand = ReactiveCommand.Create(() =>
  23. {
  24. NotificationManager.Show(new Avalonia.Controls.Notifications.Notification("Error", "Native Notifications are not quite ready. Coming soon.", NotificationType.Error));
  25. });
  26. AboutCommand = ReactiveCommand.CreateFromTask(async () =>
  27. {
  28. var dialog = new AboutAvaloniaDialog();
  29. var mainWindow = (App.Current.ApplicationLifetime as IClassicDesktopStyleApplicationLifetime)?.MainWindow;
  30. await dialog.ShowDialog(mainWindow);
  31. });
  32. ExitCommand = ReactiveCommand.Create(() =>
  33. {
  34. (App.Current.ApplicationLifetime as IClassicDesktopStyleApplicationLifetime).Shutdown();
  35. });
  36. }
  37. public IManagedNotificationManager NotificationManager
  38. {
  39. get { return _notificationManager; }
  40. set { this.RaiseAndSetIfChanged(ref _notificationManager, value); }
  41. }
  42. public ReactiveCommand<Unit, Unit> ShowCustomManagedNotificationCommand { get; }
  43. public ReactiveCommand<Unit, Unit> ShowManagedNotificationCommand { get; }
  44. public ReactiveCommand<Unit, Unit> ShowNativeNotificationCommand { get; }
  45. public ReactiveCommand<Unit, Unit> AboutCommand { get; }
  46. public ReactiveCommand<Unit, Unit> ExitCommand { get; }
  47. }
  48. }