优化更新代码,添加界面功能并整合
This commit is contained in:
33
ShrlAlgo.Toolkit.Core/Heplers/SingletonChildWindowManager.cs
Normal file
33
ShrlAlgo.Toolkit.Core/Heplers/SingletonChildWindowManager.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
using System.Windows;
|
||||
using System.Windows.Interop;
|
||||
|
||||
namespace ShrlAlgo.Toolkit.Core.Heplers;
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user