如果我已经打开了 window,如何防止 WPF Window 打开?

How can I prevent WPF Window from opening if I have an already opened window ?

我有一个打开的 window 并且我正在使用 .Show() 方法,我想阻止打开任何其他 windows 如果我有一个打开的 window .除了 ShowDialog().

当您以一种或另一种方式打开它们时,跟踪打开的数量 windows,例如:

public class WindowService
{
    private const int MaxNumberOfOpenWindows = 1;
    private int _currentNumberOfOpenWindows = 0;

    public void OpenWindow()
    {
        if (_currentNumberOfOpenWindows != MaxNumberOfOpenWindows)
        {
            Window window = new Window();
            window.Closed += Window_Closed;
            window.Show();
            _currentNumberOfOpenWindows++;
        }
    }

    private void Window_Closed(object sender, EventArgs e)
    {
        Window window = (Window)sender;
        window.Closed -= Window_Closed;
        _currentNumberOfOpenWindows--;
    }
}