Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

uwp should I be using user control inside a data template?

in my UWP app I was thinking to extract my data template into a seperate user control , i.e: <DataTemplate><local:CustomTemplate/></DataTemplate> and the user control ( customtemplate ) will have that stackpanel or grid which was previously in my DataTemplate directly, along with its bindings, I already know how to achieve this.

My Question is that by extracting out the data template in a user control, would this cause any performance hit? I read somewhere that while doing this each GridViewItem proceeds to execution of InitializeComponent() of the user control is executed and xaml is parsed on UI, which causes performance issues? But if we keep the data Template within same file ( not extracted in a user control ) then no performance issue will occurs. is this true ?

like image 904
Muhammad Touseef Avatar asked Jun 21 '17 13:06

Muhammad Touseef


1 Answers

First, if you purely want to use the same DataTemplate that uses x:Bind across different components, you don't have to wrap all your elements in either a UserControl or Custom Control.

You just need to create a resource dictionary called Templates.xaml that includes all the data templates (with x:Bind!) you want to share, as well as a cs class with the same name like this -

public partial class Templates  
{
    public Templates()
    {
        InitializeComponent();
    }
}

Then in your Templates resource dictionary, add a x:Class attribute to point to the class you just created -

<ResourceDictionary x:Class="xxx.Templates" ..>

Finally you will need to merge this resource dictionary into your App.xaml or a parent resource dictionary -

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="Fonts.xaml" />
    <ResourceDictionary Source="Brushes.xaml" />
    <ResourceDictionary Source="Styles.xaml" />
    <local:Templates />
</ResourceDictionary.MergedDictionaries>

Now build and run and it should work. Please refer to Igor's excellent post on this topic that I followed a couple of years ago which has worked for me flawlessly. I was even able to create code-behind stuff in there too.


Now back to your original question, would using a UserControl be bad for performance?

I personally believe the answer is NO in UWP.

Historically in WPF, using a UserControl inside a virtualized items control calls the InitializeComponent() inside the UserControl every time a new data template is created, which leads to performance issues so people tend to use a Custom Control instead.

However, this is no longer the case in UWP, where no matter it's a UserControl, Custom Control or even just a Grid, as long as they have the same exact child elements, they will be loaded the same amount of times. This means that the InitializeComponent inside the UserControl will only be called a small number of times until the virtualized ListView has enough numbers to recycle through the whole list.

But keep in mind that another control wrapper is an additional layer, it will always suck a little bit more memory. So unless you want additional logic on your template such as having dependency properties to show/hide stuff, you don't have to extract its content to put into another UserControl or Custom Control.

I hope my explanation makes sense.

like image 151
Justin XL Avatar answered Oct 19 '22 05:10

Justin XL