How to open a closed window?

I saw so many patterns that in order to open a closed window, I have to hide the window in closing the event, but it is not fair for me if I close the window in the middle of my work and open it again in the same window, which shows me where I left because I hide the window earlier. So how can I start a new window after closing or hide the window.

I am currently invoking the winload method, which should show a new window after calling the show method in the hide window.

private PurgeData data=new PurgeData(); private void MenuPurgeData_Click(object sender, RoutedEventArgs e) { try { if (PurgeData == null) { PurgeData = new PurgeData(); PurgeData.Show(); } else { PurgeData.WinLoad(); PurgeData.Show(); } } 

Thanks, @nagaraju.

+4
source share
2 answers

If you want hide / show behavior, you should not call Window.Close () at all in the window, but hide it by calling Window.Hide (). If the user has closed it, although closing is inevitable, you can try the following. Flip OnClosing inside the window and set e.Cancelled to true, then call .Hide (). This should allow the window to be hidden / displayed even if the user closes the window.

 // Disclaimer, untested! protected override void OnClosing(CancelEventArgs e) { e.Cancel = true; // cancels the window close this.Hide(); // Programmatically hides the window } 

EDIT:

Ok, I read your question right now ;-) So, how can I start a new window after closing or hide the window.

When you re-show the window using the above, this, of course, will be the same instance that was previously hidden, therefore, will have the same data and state. If you want completely new content, you need to create a new window () and call Window.Show (). If you hide / show as above, you will get the window back in exactly the same state until it is hidden.

Are you using the MVVM pattern in your WPF application? If so, you can try the following. Having all the data in the ViewModel and attached to the view (that is: there is no business logic or data in the code behind the window), you can call the method on the ViewModel to reset all the data when this window is shown. Your bindings will be updated, and the state of the window will be reset. Please note that this will only work if you correctly executed MVVM and linked all the elements of the main form with the ViewModel properties (including auxiliary controls).

Yours faithfully,

+8
source

It really depends on the structure of your application. Since you do not maintain state, you only need to save that type of closed window. Depending on what type of window you are using, you can assign an identifier to it (for example, in the Tag property) so that you can recognize it later. You can then click this identifier during the Closing event in the Stack . Then, if the user opens it again, pull the stack to get the identifier, and check in which window this matches. Then you can open this window.

Of course, Stack is just one data structure, and it may or may not be suitable for you. Using the stack means that the user can reopen all the last windows they closed, but you may only need one window.

Edit - base code:

 //create an enum to make it easy to recognise the type of window that was open enum WindowType { Profile, Settings }; //create a stack to hold the list of past windows Stack<WindowType> pastWindows = new Stack<WindowType>(); //give the window type a particular id profileWindow.Tag = WindowType.Profile; //open the window profileWindow.Show(); //in the closing event, if you want the user to be able to reopen this window, push it to the stack protected override void OnClosing(CancelEventArgs e) { pastWindows.Push(WindowType.Profile); //or whatever type it was base.OnClosing(e); } //to reopen the last window void ReopenLastWindow() { WindowType lastType = pastWindows.Pop(); switch(lastType) { case WindowType.Profile: profileWindow.Show(); break; case WindowType.Settings: settingsWindow.Show(); break; default: break; } } 
+1
source

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


All Articles