Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the correct way to dispose of a WPF window?

Tags:

window

wpf

I have a WPF window which I am creating from another window by calling Show(), then letting it Close() itself. When the window closes, I expect it to die, call its destructor, and delete all its child elements (such as timers..).

What is the correct way of invoking such an action?

like image 731
vanja. Avatar asked Feb 20 '09 05:02

vanja.


People also ask

How do I change a WPF window to a page?

Answers. You can either just host the Page in a Frame. Alternatively, just make a new Window, and put the same xaml content in the Window class. Migrate any code in the code behind to your new Window class.

What is the difference between page and window in WPF?

Window is the root control that must be used to hold/host other controls (e.g. Button) as container. Page is a control which can be hosted in other container controls like NavigationWindow or Frame. Page control has its own goal to serve like other controls (e.g. Button). Page is to create browser like applications.

What is window in XAML?

Advertisements. It is the root window of an XAML application which provides minimize/maximize option, Title bar, border, and close button. It also provides the ability to create, configure, show, and manage the lifetime of windows and dialog boxes.


2 Answers

Close() releases all unmanaged resources, and closes all owned Windows.

Any other managed resources you need deterministic disposal of should be handled from the Closed event.

Reference

(note: deleted previous answer, it was a completely wrong guess)

like image 172
Simon Buchan Avatar answered Sep 19 '22 13:09

Simon Buchan


There are very few WPF elements that actually need to be explicitly disposed, unlike in Windows Forms.

In the case of Window, calling Close() is sufficient to dispose all managed and unmanaged resources accorrding to the documentation.

like image 38
Samuel Jack Avatar answered Sep 22 '22 13:09

Samuel Jack