123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- using System;
- using System.Diagnostics.Tracing;
- using DialogHostAvalonia;
- using InABox.Avalonia.Components;
- using InABox.Avalonia.Router;
- namespace InABox.Avalonia;
- public interface IViewModelBase
- {
- Task Activate();
- Task Deactivate();
- bool BackButtonVisible { get; set; }
- AvaloniaMenuItemCollection PrimaryMenu { get; set; }
- AvaloniaMenuItemCollection SecondaryMenu { get; set; }
- }
- public interface IPopupViewModel
- {
- bool IsClosed { get; }
- }
- public interface IPopupViewModel<TResult> : IPopupViewModel
- {
- TResult? GetResult();
- }
- public static class Navigation
- {
- private static readonly HistoryRouter<IViewModelBase> _router;
- static Navigation()
- {
- _router = new HistoryRouter<IViewModelBase>(x => Activator.CreateInstance(x) as IViewModelBase);
- _router.CurrentViewModelChanging += (@base, direction) => CurrentViewModelChanging?.Invoke(@base, direction);
- _router.CurrentViewModelChanged += @base => CurrentViewModelChanged?.Invoke(@base);
- }
- public static void Back()
- {
- _router.Back();
- }
- public static void Navigate<T>(Action<T>? configure = null) where T : IViewModelBase
- {
- _router.GoTo<T>(configure);
- }
- public static async Task<object?> Popup<T>(Action<T>? configure = null, bool canTapAway = true)
- where T : IViewModelBase, IPopupViewModel
- {
- var viewModel = _router.InstantiateViewModel<T>(configure);
- return await Popup(viewModel, canTapAway);
- }
- public static async Task<object?> Popup<T>(T viewModel, bool canTapAway = true)
- where T : IViewModelBase, IPopupViewModel
- {
- await viewModel.Activate();
- var _result = await DialogHostAvalonia.DialogHost.Show(viewModel, (object sender, DialogClosingEventArgs eventArgs) =>
- {
- if(!canTapAway && !viewModel.IsClosed)
- {
- eventArgs.Cancel();
- }
- });
- await viewModel.Deactivate();
- return _result;
- }
- public static async Task<TResult?> Popup<T, TResult>(Action<T>? configure = null, bool canTapAway = true)
- where T : IViewModelBase, IPopupViewModel<TResult>
- {
- var viewModel = _router.InstantiateViewModel<T>(configure);
- return await Popup<T, TResult>(viewModel, canTapAway);
- }
- public static async Task<TResult?> Popup<T, TResult>(T viewModel, bool canTapAway = true)
- where T : IViewModelBase, IPopupViewModel<TResult>
- {
- await viewModel.Activate();
- var _result = await DialogHostAvalonia.DialogHost.Show(viewModel, (object sender, DialogClosingEventArgs eventArgs) =>
- {
- if(!canTapAway && !viewModel.IsClosed)
- {
- eventArgs.Cancel();
- }
- });
- await viewModel.Deactivate();
- return viewModel.GetResult();
- }
- public static void Reset<T>(Action<T>? configure = null) where T : IViewModelBase
- {
- _router.Reset<T>(configure);
- }
- public static event Action<IViewModelBase, RouterDirection>? CurrentViewModelChanging;
- public static event Action<IViewModelBase>? CurrentViewModelChanged;
- }
|