At what point do I need to Dispose my WPF user user control?

My question is about memory handling in WPF. I created a user control with notping, but the XAML witch contains only standard WPF controls. At the moment, I have not implemented code in code.

In my application, I create an instance of this user control to display to the user. When I no longer need a user control, I can simply assign it a reference variable of null value.

Do I need to write a .Dispose method and set the internal controls in my control? Or is it good practice to leave this job to the garbage collector? Thank.

+3
source share
1 answer

As a rule, in WPF you do not need to create your own controls IDisposable. Unlike Windows Forms, WPF objects are UIElementfully managed, rather than (usually) wrapping their own descriptors. Thus, they do not need to be deleted, and they can be left in the garbage collector.

That is why it UserControldoes not implement IDisposable.

There are, of course, exceptions. If your class encapsulates everything that comes from HwndHost(e.g. WebBrowser), for example, you will most likely want to make your own class IDisposableto call Dispose()on the encapsulated control. This is usually only required in interop scripts (i.e. WebBrowser, which interacts with the browser’s own controls).

+6
source

All Articles