1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- 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
- {
- var _result = await DialogHostAvalonia.DialogHost.Show(viewModel, (object sender, DialogClosingEventArgs eventArgs) =>
- {
- if(!canTapAway && !viewModel.IsClosed)
- {
- eventArgs.Cancel();
- }
- });
- 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>
- {
- var _result = await DialogHostAvalonia.DialogHost.Show(viewModel, (object sender, DialogClosingEventArgs eventArgs) =>
- {
- if(!canTapAway && !viewModel.IsClosed)
- {
- eventArgs.Cancel();
- }
- });
- 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;
- }
|