2025-02-10 20:53:40 +08:00
|
|
|
|
using System.Windows;
|
|
|
|
|
|
using System.Windows.Interop;
|
|
|
|
|
|
|
2025-04-24 20:56:44 +08:00
|
|
|
|
namespace ShrlAlgoToolkit.Core.Assists;
|
2025-02-10 20:53:40 +08:00
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|