Files
Shrlalgo.RvKits/ShrlAlgoToolkit.Core/Assists/SingletonChildWindowManager.cs

33 lines
1.3 KiB
C#
Raw Normal View History

using System.Windows;
using System.Windows.Interop;
2025-04-24 20:56:44 +08:00
namespace ShrlAlgoToolkit.Core.Assists;
public static class SingletonChildWindowManager
{
private static readonly Dictionary<Type, Window> Windows = [];
public static void ShowOrActivate<TWindow, TViewModel>(params object[] viewModelParams)
where TWindow : Window, new()
where TViewModel : class
{
var windowType = typeof(TWindow);
if (Windows.TryGetValue(windowType, out var window) && window != null)
{
window.Activate();
}
else
{
//CloseAllWindowsExcept(windowType);
Windows[windowType] = new TWindow();
Windows[windowType].Closed += (_, _) => Windows[windowType] = null;
if (Windows[windowType].DataContext == null || Windows[windowType].DataContext is not TViewModel)
{
Windows[windowType].DataContext = viewModelParams.Length == 0
? Activator.CreateInstance<TViewModel>()
: Activator.CreateInstance(typeof(TViewModel), viewModelParams);
}
_ = new WindowInteropHelper(Windows[windowType]) { Owner = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle };
Windows[windowType].Show();
}
}
}