MainWindowViewModel.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. private bool _isMenuItemChecked = true;
  12. public MainWindowViewModel(IManagedNotificationManager notificationManager)
  13. {
  14. _notificationManager = notificationManager;
  15. ShowCustomManagedNotificationCommand = ReactiveCommand.Create(() =>
  16. {
  17. NotificationManager.Show(new NotificationViewModel(NotificationManager) { Title = "Hey There!", Message = "Did you know that Avalonia now supports Custom In-Window Notifications?" });
  18. });
  19. ShowManagedNotificationCommand = ReactiveCommand.Create(() =>
  20. {
  21. NotificationManager.Show(new Avalonia.Controls.Notifications.Notification("Welcome", "Avalonia now supports Notifications.", NotificationType.Information));
  22. });
  23. ShowNativeNotificationCommand = ReactiveCommand.Create(() =>
  24. {
  25. NotificationManager.Show(new Avalonia.Controls.Notifications.Notification("Error", "Native Notifications are not quite ready. Coming soon.", NotificationType.Error));
  26. });
  27. AboutCommand = ReactiveCommand.CreateFromTask(async () =>
  28. {
  29. var dialog = new AboutAvaloniaDialog();
  30. var mainWindow = (App.Current.ApplicationLifetime as IClassicDesktopStyleApplicationLifetime)?.MainWindow;
  31. await dialog.ShowDialog(mainWindow);
  32. });
  33. ExitCommand = ReactiveCommand.Create(() =>
  34. {
  35. (App.Current.ApplicationLifetime as IClassicDesktopStyleApplicationLifetime).Shutdown();
  36. });
  37. ToggleMenuItemCheckedCommand = ReactiveCommand.Create(() =>
  38. {
  39. IsMenuItemChecked = !IsMenuItemChecked;
  40. });
  41. }
  42. public IManagedNotificationManager NotificationManager
  43. {
  44. get { return _notificationManager; }
  45. set { this.RaiseAndSetIfChanged(ref _notificationManager, value); }
  46. }
  47. public bool IsMenuItemChecked
  48. {
  49. get { return _isMenuItemChecked; }
  50. set { this.RaiseAndSetIfChanged(ref _isMenuItemChecked, value); }
  51. }
  52. public ReactiveCommand<Unit, Unit> ShowCustomManagedNotificationCommand { get; }
  53. public ReactiveCommand<Unit, Unit> ShowManagedNotificationCommand { get; }
  54. public ReactiveCommand<Unit, Unit> ShowNativeNotificationCommand { get; }
  55. public ReactiveCommand<Unit, Unit> AboutCommand { get; }
  56. public ReactiveCommand<Unit, Unit> ExitCommand { get; }
  57. public ReactiveCommand<Unit, Unit> ToggleMenuItemCheckedCommand { get; }
  58. }
  59. }