ChronoFlow/ChronoFlow.View/ViewManager.cs

62 lines
1.7 KiB
C#

using System;
using System.Collections.Generic;
using Avalonia.Controls;
namespace ChronoFlow.View
{
public class ViewManager
{
private readonly ContentControl _contentControl;
// Dictionary speichert die registrierten Views mit ihren Erstellungs-Methoden (Factories)
private readonly Dictionary<string, Func<UserControl>> _views = new();
public ViewManager(ContentControl contentControl)
{
_contentControl = contentControl;
}
/// <summary>
/// Registriert eine View mit einem eindeutigen Namen.
/// </summary>
public void Register(string name, Func<UserControl> factory)
{
if (!_views.ContainsKey(name))
{
_views[name] = factory;
}
}
/// <summary>
/// Zeigt eine registrierte View an.
/// </summary>
public void Show(string name)
{
if (_views.TryGetValue(name, out var factory))
{
var view = factory();
_contentControl.Content = view;
}
else
{
throw new InvalidOperationException($"View {name} is not registered");
}
}
/// <summary>
/// Holt eine bereits registrierte View als konkreten Typ (z. B. AdminMainView).
/// </summary>
public bool TryGetView<T>(string name, out T? view) where T : class
{
if (_views.TryGetValue(name, out var factory))
{
var instance = factory();
view = instance as T;
return view != null;
}
view = null;
return false;
}
}
}