I am developing a WPF application using MVVM with the ViewModel-First approach. A common way for page navigation in ViewModel-First is setting a ViewModel as the Content of a ContentControl. By using DataTemplates, the correct View is rendered.
Replacing the ContentControls Content can cause performance issues when the View to replace has a complex Visual Tree. It needs to be unloaded which means that the entire tree is recursively cleaned. This is explained in detail in this blogpost. I have noticed this is device dependent but can go up to 10 seconds to unload a complex page.
I have currently found 2 solutions to this problem:
- Use smaller Views or use controls with virtualization: by splitting a View into different pages or using a TabControl, the problem can be solved (partly). However the question remains, when does a View become too large?
- Use an ItemsControl which contains all previous pages, so unloading is never necessary. This solution is suggested here and here. This solution might have disadvantages with code that traverses the Visual and Logic tree and might cause other performance issues since all previous views are stored in memory.
Can the performance of unloading the ContentControl be improved without resulting to one of the mentioned solutions?