KeldorKatarn
Topic Author
Posts: 193
Joined: 30 May 2014, 10:26

DataTemplate performance

30 May 2021, 20:51

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?

Tags:
 
User avatar
sfernandez
Site Admin
Posts: 2346
Joined: 22 Dec 2011, 19:20

Re: DataTemplate performance

31 May 2021, 22:11

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.
 
KeldorKatarn
Topic Author
Posts: 193
Joined: 30 May 2014, 10:26

Re: DataTemplate performance

02 Jun 2021, 09:52

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.
 
User avatar
sfernandez
Site Admin
Posts: 2346
Joined: 22 Dec 2011, 19:20

Re: DataTemplate performance

04 Jun 2021, 10:15

And the view is the same object always that gets added/removed to/from the tree?
 
KeldorKatarn
Topic Author
Posts: 193
Joined: 30 May 2014, 10:26

Re: DataTemplate performance

04 Jun 2021, 17:32

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 :)

Who is online

Users browsing this forum: Bing [Bot] and 1 guest