WPF direct shutdown of Windows directly with Window.close causes an issue that cannot be fully exited

Source: Internet
Author: User

The other day I threw a question in the CSDN, basically described: wrote a code like this to achieve the control of the template, but found a problem, that is, when I opened a window, manually call Close (), the window is really gone, But when I closed the main window, I found that the program did not exit.

1 Private voidControltypeselectingbox_selectionchanged (Objectsender, SelectionChangedEventArgs e)2         {3             Try4             {5Type type = Controltypeselectingbox.selecteditem asType;6 7                 if(Type = =NULL)8                     Throw NewArgumentNullException ("Type is null");9 TenConstructorInfo info =type. GetConstructor (System.Type.EmptyTypes); OneControl Control = info. Invoke (NULL) asControl; A  -window window = Control asWindow; -Window Windowadnore =NULL; the  -                 //Note that the following must have an open window or a control in the grid operation, so that the following template can be displayed correctly -                 if(Window! =NULL) -                 { +Window. WindowState =System.Windows.WindowState.Minimized; -Window. ShowInTaskbar =false; +Window. Owner = This; A window. Show (); at  -Windowadnore = Application.current.windows[application.current.windows.count-1]; -                 } -                 Else -                 { -Control. Visibility =visibility.collapsed; in grid. Children.add (control); -                 } to                   +ControlTemplate template =control. Template; -  theXmlWriterSettings settings =Newxmlwritersettings (); *Settings. Indent =true; $ Panax NotoginsengStringBuilder Strbuilder =NewStringBuilder (); -XmlWriter writer =xmlwriter.create (Strbuilder, settings); the Xamlwriter.save (template, writer); +  ATxttemplatebrowser.text =Strbuilder. ToString (); the  +                 if(Window = =NULL) - grid. Children.remove (control); $                 Else $                 { - window. Close (); -Windowadnore?. Close (); the                 } -             }Wuyi             Catch(Exception ex) the             { -Txttemplatebrowser.text ="<< Error generating Template:"+ ex. Message +">>"; Wu             } -}

But if I do not call Close () manually, but instead let window call show after I click the Close button on the window, then you can completely exit (manually click on the X and then close the main window program can completely exit), then I rewrite the onclosed method in the main window

1 protected Override voidonclosed (EventArgs e)2 {3        varCollections =Application.Current.Windows;4  5         foreach(Window windowinchcollections)6         {7                if(Window! = This) 8 window. Close ();9         }Ten   One          Base. Onclosed (e); A}

It's going to be a normal exit!

Anyway asked for a lot of days no one answer, oneself try again several times, found the original is this:

  

This is the thing that opens the window and Application.Current.Windows the collection, so you see, the first is the main window, the third is our newly created window, so what's the second and fourth?

Online how to find out what this is (it is true that WPF uses a little bit), to go over the MSDN documentation:

  

Think about it, it should be WPF each time you open a window, first open the window instance, if you want to show, then Load window template (which is why window must show to see its control template), and create an invisible adornerlayer, when we manually close the window (press the Close button), it will close the Adornerlayer, but the direct close does not.

But has this magical pit really been discovered by anyone? Curious

Paste the code correctly closed:

  

1         Private voidControltypeselectingbox_selectionchanged (Objectsender, SelectionChangedEventArgs e)2         {3             Try4             {5Type type = Controltypeselectingbox.selecteditem asType;6 7                 if(Type = =NULL)8                     Throw NewArgumentNullException ("Type is null");9 TenConstructorInfo info =type. GetConstructor (System.Type.EmptyTypes); OneControl Control = info. Invoke (NULL) asControl; A  -window window = Control asWindow; -Window Windowadnore =NULL; the  -                 //Note that the following must have an open window or a control in the grid operation, so that the following template can be displayed correctly -                 if(Window! =NULL) -                 { +Window. WindowState =System.Windows.WindowState.Minimized; -Window. ShowInTaskbar =false; +Window. Owner = This; A window. Show (); at  -Windowadnore = Application.current.windows[application.current.windows.count-1]; -                 } -                 Else -                 { -Control. Visibility =visibility.collapsed; in grid. Children.add (control); -                 } to                   +ControlTemplate template =control. Template; -  theXmlWriterSettings settings =Newxmlwritersettings (); *Settings. Indent =true; $ Panax NotoginsengStringBuilder Strbuilder =NewStringBuilder (); -XmlWriter writer =xmlwriter.create (Strbuilder, settings); the Xamlwriter.save (template, writer); +  ATxttemplatebrowser.text =Strbuilder. ToString (); the  +                 if(Window = =NULL) - grid. Children.remove (control); $                 Else $                 { - window. Close (); -Windowadnore?. Close (); the                 } -             }Wuyi             Catch(Exception ex) the             { -Txttemplatebrowser.text ="<< Error generating Template:"+ ex. Message +">>"; Wu             } -}

WPF direct shutdown of Windows directly with Window.close causes an issue that cannot be fully exited

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.