Performance, Xamarin

Improving performance App using compiled bindings in Xamarin Forms

Bindings are a very good help to make the developer experience easier by allowing to dynamically load data into our controls! But I want to stop for a moment to analyze some important alerts:

➖ There’s no compile-time validation of binding expressions: By adding the values ​​in the Binding we can only verify if they are correct in RunTime, we also don’t have the help of IntelliSense, therefore, in case we misspell the name of a property, we don’t notice it until our App is executed and we don’t get the expected result.

➖ Bindings are resolved at runtime using general-purpose object inspection, in addition, the overhead of doing this varies from platform to platform. Therefore it doesn’t make the response cost-efficient.

But relax! You can solve it using Compiled Bindings! In this post, we are going to learn how to use it in an easy and simple way with Xamarin Forms!

But… First of all…🧐

What are Compiled Bindings?

Compiled Binding help us to improve performance by resolving binding expressions at compile-time rather than runtime and integrating the IntelliSense, that’s why they are resolved faster than classic bindings.

Here are some specific points that help us see the benefits we can obtain in terms of performance on average that Unit testing reveals! (⚠ These points may vary depending on the mobile device, platform, or version of the operating system in which the application is running):

When Compiled Binding use property-change notifications (OneWay, OneWayToSource or TwoWay) it has a performance approximately 8 times faster than classic Bindings.

Whereas if the Compiled Binding doesn’t use the property-change (OneTime) it has a performance approximately 20 times faster than the classic Bindings.

To understand better, let’s create an example stage!

So that we can see the result, we are going to create an example scenario, in this case, I have a ViewModel with Name and LastName properties.

Then, add the BindingContext to your MainPage.xaml.cs

And now, let’s see in the XAML!

La imagen tiene un atributo ALT vacío; su nombre de archivo es Sample_CB1.gif

I want to stop here to make some observations:

🔎 I added the Name property to the Label control and as you can see, it was not identified, instead, it was replaced by another wrong one and I had to rewrite/correct it because I didn’t have the help of the Intellisense.

🔎 In the LastName property, I made a typing error, and it let me do it without problems, but when compiling my App I will not be able to see the desired value in the Label.

Let’s start!

To avoid the inconvenience caused in the previous scenario, let’s learn how to use Compiled Binding! For its implementation you must follow the steps indicated below:

Setting the x:DataType on your .xaml:

First, Let’s create a namespace in which we will reference our project:

Then, let’s add a DataType which is an attribute that allows us to specify the data type that the components and their children will bind to.

📒 This attribute can be re-defined at any point in a view hierarchy. 

Now, if you run your application again, with the same properties of the previous scenario in which we added the name of an incorrect property, you will see this property marked in the error table and you will not be able to run your App until not having the correct properties! 🤓 You just have to fix the property name, and then,  your App is ready to run! 😎

La imagen tiene un atributo ALT vacío; su nombre de archivo es Screen-Shot-2021-09-22-at-9.03.58-PM-1024x212.png

We’re ready! 🤓  You can now use compiled bindings in your projects. 💕

If you want to know more information about Compiled Bindings you can enter here! 

Spanish post:


Tagged , , ,

3 thoughts on “Improving performance App using compiled bindings in Xamarin Forms

Leave a Reply

Your email address will not be published. Required fields are marked *