MarkSim
Topic Author
Posts: 30
Joined: 20 Apr 2021, 18:59

Load and parse component xaml only once?

26 May 2022, 08:56

I have a view where many component items (UserControls) are displayed using an ItemsControl. The DataTemplate references a UserControl I define in another xaml file. I'm finding that for every single item, it wants to reload the xaml over and over, which I assume also means parsing it over and over which is quite expensive. Is there a way to load and parse it just once other than copying the xaml into the main view?

Only thing I can think of right now is definining it in a resource file instead somehow.. Is that the best way?
 
User avatar
sfernandez
Site Admin
Posts: 2983
Joined: 22 Dec 2011, 19:20

Re: Load and parse component xaml only once?

26 May 2022, 13:43

Hi, this is something that has been asked a few times and we have plans to address this problem: #1301. The idea is storing in a cache the internal parsing of the XAML, then each time a new UserControl is requested we only have to instantiate that internal tree. That should be a lot faster than loading the XAML from the file system and parsing it every time.

In the meantime, does your user control have any code behind? If it is only a xaml encapsulating a bunch of controls then you can define a control template instead:
<Grid ...>
  <Grid.Resources>
    <ControlTemplate x:Key="MyControl" TargetType="Control">
      ...
    </ControlTemplate>
  </Grid.Resources>
  <StackPanel>
    <Control Template="{StaticResource MyControl}"/>
    <Control Template="{StaticResource MyControl}"/>
  </StackPanel>
</Grid>
 
MarkSim
Topic Author
Posts: 30
Joined: 20 Apr 2021, 18:59

Re: Load and parse component xaml only once?

27 May 2022, 09:59

Yep that worked, thanks!
 
decai
Posts: 54
Joined: 06 Jul 2016, 18:19

Re: Load and parse component xaml only once?

06 Jul 2022, 09:41

Hi,
We also has this situation, but our user control has code behind. How should we optimize it?
 
User avatar
sfernandez
Site Admin
Posts: 2983
Joined: 22 Dec 2011, 19:20

Re: Load and parse component xaml only once?

06 Jul 2022, 16:58

We also has this situation, but our user control has code behind. How should we optimize it?
Hi, if you cannot move the UserControl code to a view model, so you can convert it to a simple control template there is not much you can do now.
But loading and parsing a UserControl xaml shouldn't cost a lot, unless it is huge or it is loading lots of other resource dictionary xamls. Is that your case?

Who is online

Users browsing this forum: Google [Bot], vinick and 62 guests