Everyone likes to use apps that give a quick response, take less time to perform tasks, and work smoothly. These all factors are depended on the performance of an application. Poor performance of the app represents unresponsiveness, slow scrolling, and reduced battery life of the device. The optimizing performance also considers the user experience of the application not only the efficient code. There are many ways available to increase the performance of Xamarin.Forms Application. They can reduce the amount of work performed by CPU and memory consumption by application.
Table of Content
- 1. By enabling the XAML Compiler
- 2. Use of compile binding
- 3. Reduce unnecessary binding
- 4. Use fast Renderers
- 5. Choose Correct Layout
- 6. Create Shell Application
- 7. Use CollectionView Instead of ListView
- 8. Optimize performance of Layout
- 9. Reduce the size of the visual tree
- 10. Reduce the size of Application Resource Dictionary
- 11. Optimize ListView performance
- 12. Conclusion
Let's see the techniques that will help to improve the performance of Xamarin.Forms Application.
By enabling the XAML Compiler
XAML can be compiled directly into intermediate language with XAMLC (XAML Compiler). It provides benefits like it performs compile-time checking of XAML and notifies the user of any error. XAML element's loading and instantiation time is removed by XAML Compiler. It reduces the final assembly file size by not including .XAML file.
Use of compile binding
It will improve the performance of data binding in Xamarin.Forms Application by resolving binding expression at compile time. Compile time-binding expression generates compiled code that resolves binding 8-20 times faster than classic binding.
Reduce unnecessary binding
There is no advantage to bind data that does not need to be bound because the binding is not cost-efficient. For content, we can easily set it statically so do not use binding for it. For example, setting Button.Text="Login" Instead of binding Button.Text to view model string property has less overhead.
Use fast Renderers
Fast renderers reduce extensions and rendering the cost of controls on android by flattering the resulting native control hierarchy. It also improves performance by creating fewer objects, which turns results in a less complex visual tree and memory use.
Choose Correct Layout
With the use of layout, we can display multiple children but for single child it is meaningless to use layout.
Instead of above code we can write this code:
Also, do not use a layout which uses a combination of other layouts, because being in result unnecessary layout calculations being performed and it will affect the performance.
This is bad practice to reproduce grid layout by using multiple stack layout instances. Instead of the above code, we can write this. We can achieve the same result by using Grid instead of the combination of stack layout.
Create Shell Application
The shell application provides navigation with flyouts and tabs. It helps to avoid poor startup experience because pages are created to respond not to startup applications. Shell reduces complexity by providing fundamental features that are required in mobile application development.
Use CollectionView Instead of ListView
Collection View is a more flexible and performance alternative of ListView. It is a view that represents a list of data using different layout specifications.
Optimize performance of Layout
- keep in mind the following factors
- Use margin and padding properties.
- Whenever using Grid try to use fixed-size row and column instead of auto-size, because it will perform an additional calculation.
- If the use of HorizontalOption and VerticalOption is not required then try to avoid it.
- Try to avoid the use of relative layout because the CPU having to perform significantly more work.
- Whenever using Absolute layout if it is possible try to avoid the use of AbsoluteLayout.AutoSize property.
- Try to avoid set Label.VerticalTextAlignment property if it is not required.
Reduce the size of the visual tree
Removing the number of elements on the page will make the page render faster. There are two techniques are available to do it, the first is to hide elements and the second is to remove unnecessary elements. Hide the elements that are not visible, use IsVisible property. It will determine that element is part of the visual tree or not, if it is not visible because hidden behind other elements then it will be removed or set IsVisible to false. See the following example for the second technique.
This page layout can be achieved by following code. We can achieve this by removing elements.
Reduce the size of Application Resource Dictionary
Application Resource dictionary is used to store resources that are required throughout the application. It helps to reduce the amount of XAML parsed throughout the application.
But, XAML is specified to the page is not included in the application resource dictionary. Resources will be parsed at the startup of an application instead of at the time when it is required, so place the resources in the resource dictionary for that page not in the Application resource dictionary so it will reduce XAML parsing whenever the application starts.
Optimize ListView performance
ListView control requires the application to pass data and cell templates. How we can achieve this will have a large impact on the performance of control. When we are using ListView control number of user experiences like Initialization, Scrolling and interactions should be optimized.
The performance of an application makes a huge impact on the user experience. Everyone likes to use application with quick response, no one like unresponsiveness and slow scrolling when using the application. These factors depend on the performance of the application.
In this blog, we have seen various techniques to improve the performance of our Xamarin.Forms Application and make the application faster, smoother, and responsive.