Tips on Performance for Xamarin Forms Android and iOS
As I had to search around how to speed up Start Time of Xamarin Forms Android app, I’ve searched some blogs for general tips. As I know I need it probably later on again, I’ve wrapped some tips here.
These sources used are. If you want to know more about it, I’ve added the number of the reference to the tip so have a look there if you want to know more:
- Xamarin.Forms Performance from Microsoft, including Optimizing App Performance with Xamarin.Forms – Jason Smith from Evolve 2016.
- 5 Ways to Boost Xamarin.Forms App Startup Time from David Ortinau
- Xamarin.Forms Performance on Android by Jonathan Peppers
- Dual Splash Screen by Issac Kramer
General Tips
- (1) Reduce number of views per page
- (1) Don’t bind things if they could be set static easily
- (1) If you do not change the default, don’t set it explicit. For example, Orientation of StackLayout is “Vertical” by default. No need to set it manually if you don’t want to change it.
- (1) Transparency is expensive.
- (1) Use async/await to avoid blocking user interface.
- (1) Put code in correct events (Constructor, OnAppearing, etc.). Typically, Views should be created in Constructor, not in OnAppearing.
- (1) Do not put ListViews into ScrollViews.
- (1) If using MessagingCenter, pass a static or an instance method, not a lambda expression.
- (1) To stack elements, create a grid and add them to the same cell one after another. It’s cheaper than RelativeLayout.
- (1) Use Margins instead of Paddings.
- (1) Android only: Use Fast Renderers.
- (2) Reduce number of assemblies. Think before adding 3rd party nuget packages.
- (2) Brandon Minnick has lots of tips to improve Xamarin Build times on github.
Application start
- (1) (2) Styles defined in applications resource dictionary are processed at application start. Reducing the application resource dictionary size will therefore also reduce application start time.
- (2) Enable Ahead of Time Compilation (AOT) but unfortunately you need an enterprise license for it to activate.
- (3) Try to use ‘Link all Assemblies’ to remove unnecessary parts, at least for new apps, but maybe also for existing ones. Always do testing in Release mode also then.
- (3) On Android, enable ProGuard.
- (4) Use Dual Splash Screen to make Android Start look faster.
XAML / Code
- (1) (2) Enable Xaml Compiler
- (1) First create the child completely before you add it to a parent. For example, add all labels, images etc. to a stacklayout before you add the stacklayout to the page. XAML does it out of the box.
Layouts
- (1) Choose correct Layout, e.g. no need to add a StackLayout if it only has 1 child.
- (1) LayoutOptions.Fill or .FillAndExpand are best choice in most cases. And they are already default, so no need to set.
- (1) Autosize of rows and columns in grids should used as few as possible.
- (1) RelativeLayout is very expensive. Try to avoid.
- (1) In StackLayout, make sure there is only 1 LayoutOptions Expand.
Labels
- (1) Use FormattedText instead of multiple labels.
- (1) Use Linebreakmode NoWrap (which is already the default).
- (1) Avoid VerticalTextAlignment. Anyway, if needed, use VerticalTextAligment instead of VerticalOptions.
- (1) Avoid unnecessary updates
- (1) If updates are necessary, update multiple labels at once as they are queued together then.
ListViews
- (1) Use ListViews instead of TableViews.
- (1) Use ListViews instead of combining ScrollView and StackLayout.
- (1) Use RecycleView as ListViewCachingStrategy.
- (1) Use DataTemplateSelectors instead of doing it with BindingContext.
- (1) Provide IList instead of IEnumerable as ItemsSource.
Images
- (1) (2) Provide images in optimized width and size. Provide multiple resolutions for different usage.
- (1) Set IsOpaque to true if image is opaque.
- (1) Load Image from Content instead of Resource.
- (3) Jonathan Peppers has released glidex.forms for better imagehandling on Android.