Search code examples
xamarin.formscustom-renderer

Xamarin.Forms: Is there an event to indicate that Custom Renderer has executed?


I'm trying to address a shortcoming in an open source Xamarin.Forms control which uses custom renderers.

The issue is that I can only set certain properties on the control after the custom renderer has run. I'm just not sure when that would be. Is there any lifecycle event in a control or the page which would indicate that all custom renderers have executed?

Thank you!


Solution

  • The xxxRenderer class exposes the OnElementChanged method, which is called when the Xamarin.Forms control is created to render the corresponding native control. This method takes an ElementChangedEventArgs parameter that contains OldElement and NewElement properties. These properties represent the Xamarin.Forms element that the renderer was attached to, and the Xamarin.Forms element that the renderer is attached to .

    protected override void OnElementChanged (ElementChangedEventArgs<Entry> e)
    {
      base.OnElementChanged (e);
    
      if (Control != null) {
         // custom renderers have executed here
    
      }
    }
    

    The call to the base class's OnElementChanged method instantiates a native control(or view) , with a reference to the control being assigned to the renderer's Control property.