Navigation.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. var _result = await DialogHostAvalonia.DialogHost.Show(viewModel, (object sender, DialogClosingEventArgs eventArgs) =>
  50. {
  51. if(!canTapAway && !viewModel.IsClosed)
  52. {
  53. eventArgs.Cancel();
  54. }
  55. });
  56. return _result;
  57. }
  58. public static async Task<TResult?> Popup<T, TResult>(Action<T>? configure = null, bool canTapAway = true)
  59. where T : IViewModelBase, IPopupViewModel<TResult>
  60. {
  61. var viewModel = _router.InstantiateViewModel<T>(configure);
  62. return await Popup<T, TResult>(viewModel, canTapAway);
  63. }
  64. public static async Task<TResult?> Popup<T, TResult>(T viewModel, bool canTapAway = true)
  65. where T : IViewModelBase, IPopupViewModel<TResult>
  66. {
  67. var _result = await DialogHostAvalonia.DialogHost.Show(viewModel, (object sender, DialogClosingEventArgs eventArgs) =>
  68. {
  69. if(!canTapAway && !viewModel.IsClosed)
  70. {
  71. eventArgs.Cancel();
  72. }
  73. });
  74. return viewModel.GetResult();
  75. }
  76. public static void Reset<T>(Action<T>? configure = null) where T : IViewModelBase
  77. {
  78. _router.Reset<T>(configure);
  79. }
  80. public static event Action<IViewModelBase, RouterDirection>? CurrentViewModelChanging;
  81. public static event Action<IViewModelBase>? CurrentViewModelChanged;
  82. }