forked from amkovkov/GranuSightSoftware2
feat: move mainwindow to core and add NavigationService
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
<Window
|
||||
x:Class="GSS2.UI.Core.MainWindow"
|
||||
xmlns="https://github.com/avaloniaui"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:vm="using:GSS2.UI.Core.ViewModels"
|
||||
Title="{Binding Title}"
|
||||
x:DataType="vm:MainWindowViewModel">
|
||||
|
||||
<ContentControl x:Name="MainContent" />
|
||||
|
||||
</Window>
|
||||
@@ -0,0 +1,16 @@
|
||||
using Avalonia.Controls;
|
||||
|
||||
namespace GSS2.UI.Core;
|
||||
|
||||
public partial class MainWindow : Window
|
||||
{
|
||||
public MainWindow()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
public void SetControl(UserControl control)
|
||||
{
|
||||
MainContent.Content = control;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Controls.ApplicationLifetimes;
|
||||
|
||||
namespace GSS2.UI.Core.Services;
|
||||
|
||||
public class NavigationService
|
||||
{
|
||||
private readonly Stack<UserControl> _navigateControls = new Stack<UserControl>();
|
||||
private readonly IApplicationLifetime? _applicationLifetime;
|
||||
|
||||
public NavigationService(IApplicationLifetime? applicationLifetime)
|
||||
{
|
||||
_applicationLifetime = applicationLifetime;
|
||||
}
|
||||
|
||||
public void NavigateTo(UserControl control)
|
||||
{
|
||||
_navigateControls.Push(control);
|
||||
|
||||
switch (_applicationLifetime)
|
||||
{
|
||||
case IClassicDesktopStyleApplicationLifetime desktop
|
||||
when desktop.MainWindow is MainWindow mainWindow:
|
||||
|
||||
mainWindow.SetControl(control);
|
||||
break;
|
||||
|
||||
case ISingleViewApplicationLifetime singleView:
|
||||
singleView.MainView = control;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public void NavigateBack()
|
||||
{
|
||||
if (_navigateControls.Count <= 1)
|
||||
return;
|
||||
|
||||
var lastControl = _navigateControls.Pop();
|
||||
var control = _navigateControls.Peek();
|
||||
|
||||
switch (_applicationLifetime)
|
||||
{
|
||||
case IClassicDesktopStyleApplicationLifetime desktop
|
||||
when desktop.MainWindow is MainWindow mainWindow:
|
||||
|
||||
mainWindow.SetControl(control);
|
||||
break;
|
||||
|
||||
case ISingleViewApplicationLifetime singleView:
|
||||
singleView.MainView = control;
|
||||
break;
|
||||
}
|
||||
|
||||
if (lastControl is IDisposable disposable)
|
||||
disposable.Dispose();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user