This post is first of a series of “reminders” that will help your Xamarin.Forms apps become as performant as possible. This time we will use Fast Renderers to squeeze additional performance from our apps when running on Android.
What are Fast Renderers?
Xamarin.Forms controls are displayed in native view using Renderers. Absolute most of the Xamarin.Forms Android renderers derive from
ViewRenderer<TView, TNativeView> class which is defined as follows:
public abstract class ViewRenderer<TView, TNativeView> : VisualElementRenderer<TView>, IViewRenderer, ITabStop, AView.IOnFocusChangeListener
where TView : View
where TNativeView : AView
VisualElementRenderer<TElement> is defined as:
public abstract class VisualElementRenderer<TElement> : FormsViewGroup, IVisualElementRenderer, IEffectControlProvider
where TElement : VisualElement
FormsViewGroup is actually just a customized Xamarin.Forms class derived from
ViewGroup. The actual native Android view (like a
Button) gets embedded in this outer
ViewGroup. This is done to supply additional “global” behavior of most controls like padding, gesture handling, etc., but it also has clear performance impact – every control you create in fact generates two views in Android’s view hierarchy. This is fine for simpler app pages, but for complex and large lists, this might become a serious performance bottleneck. Luckily, Fast Renderers are here for the rescue.
When you enable Fast Renderers for your project, they will automatically replace the default renderers for the most common controls:
Fast Renderers no longer derive from
ViewRenderer<TView, TNativeView> and instead handle all layout and gesturing on the native control itself. For an example see the Fast Renderer for
Button here on GitHub.
How to enable Fast Renderers?
Fast Renderers are an experimental feature and are not enabled by default. You can enable them for the whole project by putting the following line of code before
Forms.Init() in your Android project head’s
Internally the flag for Fast Renderers is set and then initialized during
FormsAppCompatActivity as seen here in the source code.
It is important to note that Fast Renderers are only compatible with app compat activity and will have effect only on Android.