Navigation.cs 3.1 KB

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