72 lines
1.7 KiB
C#
72 lines
1.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Runtime.CompilerServices;
|
|
using System.Text;
|
|
using Microsoft.UI.Xaml.Controls;
|
|
using Microsoft.UI.Xaml.Navigation;
|
|
using WinWARP.Core.Services;
|
|
|
|
namespace WinWARP.Ui.Services;
|
|
|
|
public class NavigationService : INavigationService
|
|
{
|
|
private readonly Dictionary<Type, Type> _mappings = new();
|
|
private Frame _frame;
|
|
|
|
public void Configure(Type viewModelType, Type pageType)
|
|
{
|
|
_mappings[viewModelType] = pageType;
|
|
_mappings[pageType] = viewModelType;
|
|
}
|
|
|
|
public void RegisterFrame(Frame frame)
|
|
{
|
|
_frame = frame;
|
|
_frame.Navigated += OnFrameNavigated;
|
|
}
|
|
|
|
public bool CanGoBack => _frame?.CanGoBack ?? false;
|
|
|
|
public void GoBack()
|
|
{
|
|
if (CanGoBack)
|
|
{
|
|
_frame.GoBack();
|
|
}
|
|
}
|
|
|
|
public void NavigateTo<TViewModel>(object parameter = null) where TViewModel : class
|
|
{
|
|
var viewModelType = typeof(TViewModel);
|
|
|
|
if (!_mappings.TryGetValue(viewModelType, out var pageType))
|
|
{
|
|
throw new ArgumentException($"No registered page found for ViewModel: {viewModelType.FullName}");
|
|
}
|
|
|
|
if (_frame != null)
|
|
{
|
|
_frame.Navigate(pageType, parameter);
|
|
}
|
|
}
|
|
|
|
public Type CurrentViewModelType
|
|
{
|
|
get; private set;
|
|
}
|
|
|
|
public event EventHandler<Type> NavigatedToViewModel;
|
|
|
|
private void OnFrameNavigated(object sender, NavigationEventArgs e)
|
|
{
|
|
var pageType = e.SourcePageType;
|
|
|
|
if (_mappings.TryGetValue(pageType, out var viewModelType))
|
|
{
|
|
CurrentViewModelType = viewModelType;
|
|
|
|
NavigatedToViewModel?.Invoke(this, viewModelType);
|
|
}
|
|
}
|
|
} |