Window.Show关闭应用程序

时间:2018-03-16 22:33:26

标签: c# wpf

我有一个应用程序,我一度关闭所有窗口,我想重新启动其中一个关闭的窗口。但问题是,当我说Window.Show();断点显示实际高度和实际宽度为0时,在执行该行之后,整个应用程序关闭。为什么,它是垃圾收集?

private void btnCancel_Click(object sender, RoutedEventArgs e)
{
    if (jsonAssayViewModel.IsLiveProgress)
    {
        var res = Xceed.Wpf.Toolkit.MessageBox.Show(
                   Application.Current.TryFindResource("CloseRun").ToString(),
                   "Confirm dialog",
                   MessageBoxButton.YesNo,
                   MessageBoxImage.Question
               );
        if (res == MessageBoxResult.No)
        {
            jsonAssayViewModel.PreventClosingWindow = true;
            return;
        }
    }
    //Raise the close event....
    if (CloseCurrentProgressWindow != null)
    {
        CloseCurrentProgressWindow();
    }
    var window = Window.GetWindow(this);
    if (window != null && window.Tag.ToString() == "Success")
    {
        if ((sender as Button).Content.ToString() == Application.Current.TryFindResource("Done").ToString())
        {
            var AllWindows = Application.Current.Windows.Cast<Window>()
                                               .Where(win => win.IsLoaded);
            if (AllWindows.Count() > 2)
            {
                jsonAssayViewModel.SelectedAssay = null;
                jsonAssayViewModel.SelectedVolume = string.Empty;
                foreach (Window win in App.Current.Windows)
                {
                    win.Close();
                }
                NextGenDGRunSetupWindow wn = new NextGenDGRunSetupWindow();
                wn.Show();
            }
        }
    }
}

在NextGenDGRunSetupWindow中我有以下

public NextGenDGRunSetupWindow()
{
    InitializeComponent();
    //idleTimeDetector = new IdleDetector(this); //1 minute
    readJsonViewModel = ReadJsonAssayViewModel.ReadJsonAssayViewModelInstance;
    //idleTimeDetector.IsIdle += IdleTime_IsIdle;
    readJsonViewModel.LaunchErrorWindowFromAnywhere += ReadJsonViewModel_LaunchErrorWindowFromAnywhere;
}

1 个答案:

答案 0 :(得分:2)

问题是您要关闭应用程序中加载的每个Window。由于Show()未阻止,因此方法完成执行启动窗口的那一刻关闭,您的应用程序退出。你有几个选择:

  1. 使用wn.ShowDialog(); - 这将阻止当前方法,直到窗口关闭;或
  2. 请勿在{{1​​}}。
  3. 中关闭主窗口,无论哪个窗口
  4. Application.ShutDownMode更改为foreachShutDownMode.OnExplicitShutdown
相关问题