如何在窗口后加载2次?

时间:2014-03-13 09:41:48

标签: c# wpf

现在已经有一段时间寻找这个问题的解决方案,但我似乎无法找到答案。

我有一个MainWindow,我想从一个按钮加载“gameWindow”2次。我想在第一次关闭后加载第二个窗口。

如果你能想象这个想法:

private void Action_Click(object sender, RoutedEventArgs e)
{
    if (blueTeamMap != null && redTeamMap != null)
    {
        GameWindow matchBlueTeam = new GameWindow(playersBlueTeam, playersRedTeam, blueTeamMap);
        matchBlueTeam.Show();
        while (matchBlueTeam != null)
        {
            // wait for window to resign
        }

        GameWindow matchRedTeam = new GameWindow(playersBlueTeam, playersRedTeam, redTeamMap);
        matchRedTeam.Show();
    }
}

3 个答案:

答案 0 :(得分:2)

执行以下操作:将代码编辑为此类

//your code here.

//add event here
matchBlueTeam.FormClosed += FormName_FormClosed;
matchBlueTeam.Show();

添加事件处理程序:

private void FormName_FormClosed(object sender, System.Windows.Forms.FormClosedEventArgs e)
{
   //unregister first window from event
   ((GameWindow)sender).FormClosed -= FormName_FormClosed;

   if (blueTeamMap != null && redTeamMap != null)
   {
      //pop second window
      GameWindow matchRedTeam = new GameWindow(playersBlueTeam, playersRedTeam,     redTeamMap);
      matchRedTeam.Show();
   }
}

答案 1 :(得分:1)

假设matchBlueTeam窗口关闭,GameWindowClosed个事件,例如:

private void Action_Click(object sender, RoutedEventArgs e)
{
    if (blueTeamMap != null && redTeamMap != null)
    {
        GameWindow matchBlueTeam = new GameWindow(playersBlueTeam, playersRedTeam, blueTeamMap);
        matchBlueTeam.Closed += (s,e) => { 
            GameWindow matchRedTeam = new GameWindow(playersBlueTeam, playersRedTeam, redTeamMap);
            matchRedTeam.Show();
        };
        matchBlueTeam.Show();

    }
}

答案 2 :(得分:0)

您可以在窗口方法上询问ShowDialog()。它等待窗户关闭。

Window1 w1 = new Window1();
w1.ShowDialog();
Window2 w2 = new Window2();
w2.ShowDialog();

或有事件:

        //////Code    
        Window1 w1 = new Window1();
        w1.Closed += v_Closed;
        w1.Show();
    }


    void v_Closed(object sender, EventArgs e)
    {
        Window1 w1 = (Window1)sender;
        w1.Closed -= v_Closed;
        Window2 w2 = new Window2();
        w2.Show();
    }