【问题标题】:How to stop MainWindow from closing whole application如何阻止 MainWindow 关闭整个应用程序
【发布时间】:2016-03-18 08:37:08
【问题描述】:

我正在尝试从我的 WPF 应用程序中的子窗口关闭我的主窗口。问题是,一旦我尝试“关闭”主窗口,我的整个应用程序就会关闭。

这是我在主窗口(pgLogin)中的编码:

Window nextWindow = null;
nextWindow = new pgDashboard();
nextWindow.Owner = this;
this.Hide();
nextWindow.Show();

还有我的子窗口(pgDashboard):

public static T IsWindowOpen<T>(string name = null) where T : Window
{
    var windows = Application.Current.Windows.OfType<T>();
    return string.IsNullOrEmpty(name) ? windows.FirstOrDefault() : windows.FirstOrDefault(w => w.Name.Equals(name));
}


private void HAZEDashboard_Loaded(object sender, RoutedEventArgs e)
{
    var credentials = this.Owner as pgLogin;
    credentials.txtEmailAddress.Text.ToString();

    var window = IsWindowOpen<pgLogin>();

    if (window != null)
    {
        window.Close();
    }
}

有没有办法关闭主窗口而不隐藏它并且仍然保持打开我的子窗口?

【问题讨论】:

  • 如果您使用的是 MDI 表单。然后在关闭时你必须打开表格

标签: c# wpf window


【解决方案1】:

转到应用程序 App.xaml 并将“ShutdownMode”更改为“OnExplicitShutdown”。 默认值为 ShutdownMode="OnMainWindowClose",这会导致您描述的行为。

【讨论】:

    猜你喜欢
    • 2019-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-02
    相关资源
    最近更新 更多