mirror of
https://github.com/real-zony/ZonyLrcToolsX.git
synced 2026-02-02 09:08:26 +00:00
feat: Create a new GUI project based on Avalonia. (Powered by Claude)
This commit is contained in:
50
src/ZonyLrcTools.Desktop/Services/NavigationService.cs
Normal file
50
src/ZonyLrcTools.Desktop/Services/NavigationService.cs
Normal file
@@ -0,0 +1,50 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using ZonyLrcTools.Desktop.ViewModels;
|
||||
|
||||
namespace ZonyLrcTools.Desktop.Services;
|
||||
|
||||
public class NavigationService : INavigationService
|
||||
{
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
private readonly Stack<ViewModelBase> _navigationStack = new();
|
||||
|
||||
public ViewModelBase? CurrentViewModel => _navigationStack.TryPeek(out var vm) ? vm : null;
|
||||
public bool CanGoBack => _navigationStack.Count > 1;
|
||||
|
||||
public event EventHandler<ViewModelBase>? NavigationChanged;
|
||||
|
||||
public NavigationService(IServiceProvider serviceProvider)
|
||||
{
|
||||
_serviceProvider = serviceProvider;
|
||||
}
|
||||
|
||||
public TViewModel NavigateTo<TViewModel>() where TViewModel : ViewModelBase
|
||||
{
|
||||
var viewModel = _serviceProvider.GetRequiredService<TViewModel>();
|
||||
|
||||
if (_navigationStack.TryPeek(out var current))
|
||||
{
|
||||
_ = current.OnDeactivatedAsync();
|
||||
}
|
||||
|
||||
_navigationStack.Push(viewModel);
|
||||
_ = viewModel.OnActivatedAsync();
|
||||
|
||||
NavigationChanged?.Invoke(this, viewModel);
|
||||
return viewModel;
|
||||
}
|
||||
|
||||
public void GoBack()
|
||||
{
|
||||
if (!CanGoBack) return;
|
||||
|
||||
var current = _navigationStack.Pop();
|
||||
_ = current.OnDeactivatedAsync();
|
||||
|
||||
if (_navigationStack.TryPeek(out var previous))
|
||||
{
|
||||
_ = previous.OnActivatedAsync();
|
||||
NavigationChanged?.Invoke(this, previous);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user