Page 1 of 1
DataTemplate performance
Posted: 30 May 2021, 20:51
by KeldorKatarn
Since I'm using DataTemplates for matching ViewModels to Views currently in Caliburn.Noesis, I was wondering how performance heavy it is to re-create datatemplate instances when screens are changes rapidly. Is there some caching going on in Noesis that already deals with the performance impact or should I think about some ways to cache stuff in the framework?
Re: DataTemplate performance
Posted: 31 May 2021, 22:11
by sfernandez
Noesis is not caching anything, but it is is fast to apply DataTemplates as long as they are already in memory (in any loaded resource dictionary), because we are just cloning the visual tree of the template.
But it ultimately depends on the complexity of the template. If you plan to load complex screens using data templates maybe it is better to have the main screens already in a root xaml and just switch the visibility of each one accordingly.
Re: DataTemplate performance
Posted: 02 Jun 2021, 09:52
by KeldorKatarn
I found a good solution :) I'm mixing Caliburn.Micro's approach with mine. I still generate DataTemplates for each ViewModel, but they no longer contain the entire view UserControl.
Instead they have a ContentPresenter with an attachedProperty which connects to the ServiceProvider to resolve the view. The serviceLocator is injected into the DataTemplate as a resource.
Re: DataTemplate performance
Posted: 04 Jun 2021, 10:15
by sfernandez
And the view is the same object always that gets added/removed to/from the tree?
Re: DataTemplate performance
Posted: 04 Jun 2021, 17:32
by KeldorKatarn
Yes. I also added something in case the view is used in multiple places against the same viewmodel. The framework now automatically adds an attached property containing a location ID to the parent Visual so the framework caches them in the viewmodel by location. but yes the UserControl always gets injected into a new contentpresenter wherever the datatemplate is instantiated. So the only thing Noesis needs to re-instantiate for the datatemplate is only the ContentPresenter control, nothing else. Everything below it gets injected by the framework as either a IoC lookup or a cached instance. Works great :)