ChronoFlow/ChronoFlow.View/ViewManager.cs
2025-04-27 21:28:33 +02:00

49 lines
1.3 KiB
C#

using System;
using System.Collections.Generic;
using Avalonia.Controls;
using Microsoft.Data.Sqlite;
namespace ChronoFlow.View
{
///<summary>
/// Verwaltet alle Views der Anwendung und wechselt sie bei Bedarf.
/// </summary>
public class ViewManager
{
private readonly ContentControl _targetControl;
private readonly Dictionary<string, Func<UserControl>> _registieredViews = new();
public ViewManager(ContentControl targetControl)
{
_targetControl = targetControl;
}
///<summary>
/// Registriert eine View mit einem Namen
/// </summary>
public void Show(string name, Func<UserControl> viewFactory)
{
_registieredViews[name] = viewFactory;
}
///<summary>
/// Zeigt die View mit dem gegebenen Namen an.
/// </summary>
public void Show(string name)
{
if(_registieredViews.TryGetValue(name, out var factory))
_targetControl.Content = factory();
else
{
throw new InvalidOperationException($"View {name} is not registered");
}
}
public void Register(string name, Func<UserControl> viewFactory)
{
_registieredViews[name] = viewFactory;
}
}
}