Disposing View Models - WPF MVVM NAVIGATION TUTORIAL #6

preview_player
Показать описание
Learn how and why to dispose view models in a WPF application. This is crucial for preventing memory leaks from view models never getting garbage collected. I also explain why it is preferable to make view model lifetimes transient rather than singleton.

Navigation is an essential piece of an application. In this series, I demonstrate how to implement various navigation scenarios in a WPF MVVM application.

TIMESTAMPS:
0:00 - Introduction
0:22 - Setting Up Disposal Scenario
3:25 - Memory Leak Investigation
4:17 - Implementing Dispose
5:40 - Propagating Disposals
6:37 - Disposing the NavigationBarViewModel
8:18 - Making View Models Transient
11:04 - Conclusion

OTHER LINKS:
Рекомендации по теме
Комментарии
Автор

A great video Sean. I've overlooked this issue in the past but now I'm working on larger applications I'll definitely be working this technique into my projects.

tpbarratt
Автор

Awesome and nice work.... When you include the Navigation in your MVVM Essentials will be great..

elkhayyat
Автор

Thanks for great course! I have question, why destructor is called for LayoutViewModel but it is not called for AccountViewModel or HomeViewModel

viktoryakushenko
Автор

I don't hit the destructor sometimes when I go to home... or does the garbage collector not disposing it the moment I hit the home button (like... does it take time for the garbage collector to dispose the AccountViewModel?)

zerosandones
Автор

Question: why do you declare events with Action instead of EventHandler? Is it just for the simpler parameters on the handler?

jackkendall
Автор

Views are garbage collected but viewmodels must be explicitly disposed. Set all fields and events to null (especially PropertyChanged) because hidden view objects that retain mystery bindings will be your biggest source of memory leaks. And viewmodels will be hard to manage unless you give them a parent-child relationship just like the visual and logical trees. Destructors don't come into play with viewmodels because viewmodels should only be used and disposed on the Dispatcher they were created from.

welcome to shbcf.ru