A few days ago, a friend came to me. He asked me for some recommendations on how to make the application he actually was developing with Xamarin Forms as efficient as possible in terms of performance.
My recommendations are bit aerial and unstructured, but then I kept thinking about it, and then, I remembered that a few months ago I was reading a very friendly article about it.
Before continue to reading, certain time ago I wrote an article about Xamarin forms performance, give it a read please.
I know that many times we begin to develop focused mainly on the functions and then in the testing phase. We begin to adjust those things that should be improved, because in the practice it depends on the scenario and a large number of conditions. It’s good to start considering the following list of recommendations that I extracting today for you from many sources, let’s start.
- Enable XAML compilation:
- Do not use BIND when is enough with just a simple static value assignment.
- Do not assign values that are equal to default values. It doesn’t makes sense to set something that is already there.
- Avoid the use of transparency, you can achieve the same result using full opacity instead.
- This is generally known and accepted: Always you can, use async/await.
- Do not use ContentView, with Padding you can achieve a better result, also applying margins to child elements.
- Do not use a StackLayout for handling just one element inside.
- Grid is expensive, don’t use it unless necessary. You can obtain the same result with the StackLayout. The same applies using many StackLayouts when only one Grid can do the job.
- Be careful with the property called Spacing (ColumnSpacing/RowSpacing for Grid) and the Padding. Instead of this:
<ContentView Padding="10,10,10,5"> <Label Text="1"/> </ContentView> <ContentView Padding="10,0,10,5"> <Label Text="2"/> </ContentView> <ContentView Padding="10,0,10,0"> <Label Text="3"/> </ContentView>
Is better to use this:
<StackLayout Padding="10" Spacing="5"> <Label Text="1"/> <Label Text="2"/> <Label Text="3"/> </StackLayout>
An the list may continue, but my last recommendation is to enable the profiler and determine how the UI impacts your application performance. This is very important because once in production you won’t have the users complaining about their battery dying or application getting unresponsive.
Feel free to comment and see you in the next post.