MultiBindings - XAML vs code-behind

pugthegreat 11 Reputation points
2022-08-18T23:53:03.797+00:00

I have noticed when using MultiBindings in XAML, the Converter is called multiple times.

However, when defining the same MultiBinding in code-behind, the Converter is called just one time.

<StackLayout.BackgroundColor>  
    <MultiBinding Converter="{StaticResource TestConverter}">  
        <Binding Path="Var1"/>  
        <Binding Path="Var2"/>  
        <Binding Path="Var3"/>  
        <Binding Path="Var3_1"/>  
        <Binding Path="Var3_2"/>  
    </MultiBinding>  
</StackLayout.BackgroundColor>  
  

In the example XAML code above, the Converter gets called up to 8 times, until all bindings are loaded.

However, when setting the same MultiBinding in the code-behind:

mainStackLayout.SetBinding(StackLayout.BackgroundColorProperty, new MultiBinding  
   {  
       Bindings = new Collection<BindingBase>  
       {  
            new Binding("Var1"),  
            new Binding("Var2"),  
            new Binding("Var3"),  
            new Binding("Var3_1"),  
            new Binding("Var3_2"),  
        },  
        Converter = converter  
   });  

 

The Converter gets called just one time, with all values loaded.

Doesn't this means, that MultiBindings set in the code-behind are actually better performant than in XAML?

Suppose you have a MultiBinding with 15 Bindings. The converter will get called +15 times. That is +15 instances of the Converter being created, as opposed to just 1 instance, if the MultiBinding is set in the code-behind. Now imagine you have multiple of those MultiBindings in just one page. How noticeable would the difference be?

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,326 questions
{count} vote