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?
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.
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.
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.
Close()
releases all unmanaged resources, and closes all owned Window
s.
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)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With