Navigation.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using System;
  2. using System.Diagnostics.Tracing;
  3. using DialogHostAvalonia;
  4. using InABox.Avalonia.Components;
  5. using InABox.Avalonia.Router;
  6. namespace InABox.Avalonia;
  7. public interface IViewModelBase
  8. {
  9. Task Activate();
  10. Task Deactivate();
  11. bool BackButtonVisible { get; set; }
  12. bool ProgressVisible { get; set; }
  13. AvaloniaMenuItemCollection PrimaryMenu { get; set; }
  14. AvaloniaMenuItemCollection SecondaryMenu { get; set; }
  15. }
  16. public interface IPopupViewModel
  17. {
  18. bool IsClosed { get; }
  19. }
  20. public interface IPopupViewModel<TResult> : IPopupViewModel
  21. {
  22. TResult? GetResult();
  23. }
  24. public static class Navigation
  25. {
  26. private static readonly HistoryRouter<IViewModelBase> _router;
  27. static Navigation()
  28. {
  29. _router = new HistoryRouter<IViewModelBase>(x => Activator.CreateInstance(x) as IViewModelBase);
  30. _router.CurrentViewModelChanging += (@base, direction) => CurrentViewModelChanging?.Invoke(@base, direction);
  31. _router.CurrentViewModelChanged += @base => CurrentViewModelChanged?.Invoke(@base);
  32. }
  33. public static void Back()
  34. {
  35. _router.Back();
  36. }
  37. public static void Navigate<T>(Action<T>? configure = null) where T : IViewModelBase
  38. {
  39. _router.GoTo<T>(configure);
  40. }
  41. public static async Task<object?> Popup<T>(Action<T>? configure = null, bool canTapAway = true)
  42. where T : IViewModelBase, IPopupViewModel
  43. {
  44. var viewModel = _router.InstantiateViewModel<T>(configure);
  45. return await Popup(viewModel, canTapAway);
  46. }
  47. public static async Task<object?> Popup<T>(T viewModel, bool canTapAway = true)
  48. where T : IViewModelBase, IPopupViewModel
  49. {
  50. await viewModel.Activate();
  51. var _result = await DialogHostAvalonia.DialogHost.Show(viewModel, (object sender, DialogClosingEventArgs eventArgs) =>
  52. {
  53. if(!canTapAway && !viewModel.IsClosed)
  54. {
  55. eventArgs.Cancel();
  56. }
  57. });
  58. await viewModel.Deactivate();
  59. return _result;
  60. }
  61. public static async Task<TResult?> Popup<T, TResult>(Action<T>? configure = null, bool canTapAway = true)
  62. where T : IViewModelBase, IPopupViewModel<TResult>
  63. {
  64. var viewModel = _router.InstantiateViewModel<T>(configure);
  65. return await Popup<T, TResult>(viewModel, canTapAway);
  66. }
  67. public static async Task<TResult?> Popup<T, TResult>(T viewModel, bool canTapAway = true)
  68. where T : IViewModelBase, IPopupViewModel<TResult>
  69. {
  70. await viewModel.Activate();
  71. var _result = await DialogHostAvalonia.DialogHost.Show(viewModel, (object sender, DialogClosingEventArgs eventArgs) =>
  72. {
  73. if(!canTapAway && !viewModel.IsClosed)
  74. {
  75. eventArgs.Cancel();
  76. }
  77. });
  78. await viewModel.Deactivate();
  79. return viewModel.GetResult();
  80. }
  81. public static void Reset<T>(Action<T>? configure = null) where T : IViewModelBase
  82. {
  83. _router.Reset<T>(configure);
  84. }
  85. public static event Action<IViewModelBase, RouterDirection>? CurrentViewModelChanging;
  86. public static event Action<IViewModelBase>? CurrentViewModelChanged;
  87. }