简体   繁体   中英

Close Window after open another window?

I'm work on WPF project, let say I have two windows window1,window2, window1 call window2: in window1 code behind :

Window2 _window2 = new Window2();
_window2.ShowDialog();

What I want to know how I close first window (window1) after the secound window (window2) is opened ?

Before you can close the main window (if it is), then you need to tell the app first which is the main window. Depending upon your shut down mode, if you close the main window, you shut down the app. You can set this using Application.Current.MainWindow property. See Here.

If you wish to close the first window after opening the second, you will need to hook up an event, or better still use a decoupled messaging system to inform the first window the second has opened.

Form1:

Window2 _window2 = new Window2();
this.Hide(); //Hides Form 1
_window2.ShowDialog();

Also see: Close a Window from Another In Wpf if you wanna have window1 closed.

If "Window1" is not your main window you can just type this

Window2 _window2 = new Window2();
_window2.Show();
this.Close();

If your form is the main form than refer ( Windows Forms: Change application mainwindow at runtime )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM