[FIXED] Difference between e.NewElement and Control?

Issue

So when making a custom renderer , you do most of your work inside the overriden OnElementChanged() function.I want to make a custom renderer for the Xamarin.Forms.Picker control:

 protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)

what is here the difference between e.NewElement and Control?The second one seems to be inherited from ViewRenderer(PickerRenderer in my case).I also don’t quite understand what e.OldElement does, maybe the reference to my last custom Picker? Thanks.

Solution

  • Control (this.Control) is the PLATFORM control. E.g. on Android, it is a subclass of Android.Views.View.
  • Element (this.Element) refers to the XAMARIN FORMS cross-platform element/view. It is a subclass of Xamarin.Forms.View.
  • e.NewElement is always identical to this.Element.
  • e.OldElement is the previous value that was attached to this custom renderer. It will be null when the renderer is created.
  • When the renderer is disposed, OnElementChanged might be called again, with e.OldElement being the XF element that is going away. In this case, e.NewElement is null.

IMPORTANT: In OnElementChanged, always check whether e.NewElement is null. If it is, do nothing. (Or call any "cleanup" code, if you need to do something when "going away"/disposing.)

Answered By – ToolmakerSteve

Answer Checked By – Mildred Charles (Easybugfix Admin)

Leave a Reply

(*) Required, Your email will not be published