How to re-open the closed window?

梦想与她 提交于 2020-01-24 07:46:36

问题


i've seen so many samples that in order to open closed window i should hide the window in closing event, but this is not fair for me if i close window in middle of my work and again open same window it showing me content where i left because i'm hiding window previously. So how can i start freshly my window after closed or hide the window.

currently i'm calling winload method which is to show fresh window after calling the show method of a 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.


回答1:


If you want the behaviour of hide/show, you should not call Window.Close() on the window at all, but hide it by calling Window.Hide(). If the user has closed it though and a close is unavoidable, you can try the following. Override OnClosing inside the Window and set e.Cancelled to true, then call .Hide(). This should allow the window to be hidden/shown 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've now read your question properly ;-) So how can i start freshly my window after closed or hide the window.

When you re-show the window using the above, it will of course be the same instance as the one that was previously hidden, hence will have the same data and state. If you want completely new contents you need to create a new Window() and call Window.Show() on that. If you hide/show as above then you'll get the window back in exactly the same state before it was hidden.

Are you using the MVVM pattern in your WPF application? If so, you could try the following. By having all the data in your ViewModel and bound to by the View (ie: no business logic or data in the code behind of the Window), then you could invoke a method on the ViewModel to reset all data when the window is shown. Your bindings will refresh and the window state will be reset. Note this will only work if you have correctly followed MVVM and bound all elements on the main form to ViewModel properties (including sub controls).

Best regards,




回答2:


It really depends on the structure of your app. Since you're not maintaining state, you only need to save the actual type of window that was closed. Depending on what type of window you're using, you can assign it an ID (e.g. in its Tag property) so that it can be recognized later. You can then push this ID during the Closing event in a Stack. Then, if the user reopens it, pop the stack to get the ID and check what window that corresponds to. You can then reopen that window.

Of course, Stack is just one data structure and it may or may not be suitable for you. Using a Stack means that user can reopen all the past windows they closed, but maybe you might just want the one window instead.

Edit - basic 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;
   }
}



回答3:


 this.Opacity = 0 

to "close the window"

 this.Opacity = 1 

to "re-open it"

A remark concerning the Hide() method: from another class, the window will in fact be considered as closed and the code will continue after a ShowDialog() method usage. Using the "Opacity" property overrides the problem.



来源:https://stackoverflow.com/questions/8741632/how-to-re-open-the-closed-window

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!