启动新窗口时如何关闭当前窗口(在代码中) [英] How to close current window (in Code) when launching new Window

查看:31
本文介绍了启动新窗口时如何关闭当前窗口(在代码中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SignInWindow signIn= new SignInWindow();
signIn.ShowDialog();

以上代码在我的 MainWindow 类中.

The above code is in my MainWindow class.

当显示新窗口时,我希望关闭当前窗口.最好的方法是什么?

When the new Window is shown, I want the current window to close. Whats the best way to do this?

我的应用程序是一个 C# WPF 应用程序

My application is a C# WPF application

我已经尝试过这个,但是当它被调用时,我的应用程序退出

I've tries this but when it's called, my application exits

    static private void CloseAllWindows()
    {
        for (int intCounter = App.Current.Windows.Count - 1; intCounter >= 0; intCounter--)
            App.Current.Windows[intCounter].Close();
    }

推荐答案

只需这样做:

this.Close();
SignInWindow signIn = new SignInWindow();
signIn.ShowDialog();

请记住,这实际上会关闭MainWindow.如果您真的只是想隐藏它,那么请执行以下操作:

bear in mind that will actually close the MainWindow. If all you're really trying to do is hide it, then do this:

this.Hide();
SignInWindow signIn = new SignInWindow();
signIn.ShowDialog();
this.Show();

这将在登录表单启动时隐藏MainWindow,但是当它完成时显示.

That will hide the MainWindow while the login form is up, but then show it again when it's complete.

好的,显然您是从外部表单的静态类启动此表单.那将是非常相关的信息.但解决方案是这样的:

Okay, so apparently you're launching this form from a static class that is outside the form. That would have been pretty relevant information. But a solution would be this:

var w = Application.Current.Windows[0];
w.Hide();

SignInWindow signIn = new SignInWindow();
signIn.ShowDialog();

w.Show();

这篇关于启动新窗口时如何关闭当前窗口(在代码中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆