如果我已打开窗口,如何阻止WPF窗口打开?

时间:2018-05-31 11:51:20

标签: wpf mvvm prism-4

我有一个打开的窗口,我正在使用.Show()方法,我想阻止任何其他窗口打开如果我有一个打开的窗口。除了ShowDialog()之外的东西。

1 个答案:

答案 0 :(得分:1)

在您以某种方式打开时,跟踪打开的窗口数量,例如:

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--;
    }
}