关闭所有活动窗口并打开新窗口

时间:2021-02-19 07:35:58

标签: c# wpf

我做了一些应用程序,然后我需要在 C# 中关闭所有活动窗口的函数

但是在那之后我需要打开一个新的

所以我的代码是..


 for (int intCounter = App.Current.Windows.Count - 1; intCounter >= 0; intCounter--)
                App.Current.Windows[intCounter].Close();
            Dashboard mf2 = new Dashboard();
            mf2.mastersupplier_clicked(sender, new RoutedEventArgs());
            mf2.ShowDialog();

但是我的应用程序退出了并且从不显示仪表板对话框......对此有什么想法吗?我卡在这个功能里了。

我用 wpf c# 做这个

2 个答案:

答案 0 :(得分:1)

在关闭窗口之前将 ShutdownModeApp 设置为 ShutdownMode.OnExplicitShutdown

App.Current.ShutdownMode = ShutdownMode.OnExplicitShutdown;

for (int intCounter = App.Current.Windows.Count - 1; intCounter >= 0; intCounter--)
    App.Current.Windows[intCounter].Close();

Dashboard mf2 = new Dashboard();
mf2.mastersupplier_clicked(sender, new RoutedEventArgs());
//shut down the app explicitly when the dashboard dialog is closed:
mf2.Closed += (ss, ee) => App.Current.Shutdown();
mf2.ShowDialog();

答案 1 :(得分:0)

测试这段代码

Dashboard mf2 = new Dashboard();
mf2.mastersupplier_clicked(sender, new RoutedEventArgs());
mf2.Show();
for (int intCounter = App.Current.Windows.Count - 1; intCounter >= 0; intCounter--)
{
   var wnd = App.Current.Windows[intCounter] as Window;
   if(wnd != mf2)
   {
       wnd.Owner = mf2;
       wnd.Close();
   }
}