How to close the current window (in code) when starting a new window

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

The above code is in my MainWindow class.

When a new window appears, I want to close the current window. What is the best way to do this?

My application is a C # WPF application


I try to do this, but when it is called, my application terminates

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

Just do the following:

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

remember to actually close MainWindow . If all you are really trying to do is hide it, then do the following:

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

This will hide MainWindow until the login form is completed, and then show it again when it is complete.


Ok, so apparently you are running this form from a static class that is outside . This would be very relevant information. But the solution would be this:

 var w = Application.Current.Windows[0]; w.Hide(); SignInWindow signIn = new SignInWindow(); signIn.ShowDialog(); w.Show(); 
+12
source

This is a kind of redirect code. it closes the current window / page and redirects to a new page / window in wpf

 SignWindow sw = new SignWindow(); this.Content = sw; 
+2
source

You can try the following:

 SignInWindow signIn= new SignInWindow(); Application.Current.Windows[0].Close(); signIn.ShowDialog(); 
0
source

Mike Perrenauโ€™s answer is 100% perfect, although you can create a new window object and activate it before closing the main window.

0
source

Source: https://habr.com/ru/post/955492/


All Articles