AlexSJC
发布于 2023-12-14 / 4 阅读
0
0

C# UniqueWindow

#C#
using System.Collections.Generic;
using System.Windows;

public class UniqueWindow
{
    private static readonly List<Window> windows = new();

    public static T Get<T>() where T : Window, new()
    {
        var window = windows.Find(w => w is T);
        if (window == null)
        {
            window = new T();
            window.Closed += (_, _) => windows.Remove(window);
            windows.Add(window);
        }
        return (T)window;
    }
}


评论