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.
CodePudding user response:
Control
(this.Control
) is the PLATFORM control. E.g. on Android, it is a subclass ofAndroid.Views.View
.Element
(this.Element
) refers to the XAMARIN FORMS cross-platform element/view. It is a subclass ofXamarin.Forms.View
.e.NewElement
is always identical tothis.Element
.e.OldElement
is the previous value that was attached to this custom renderer. It will benull
when the renderer is created.- When the renderer is disposed,
OnElementChanged
might be called again, withe.OldElement
being the XF element that is going away. In this case,e.NewElement
isnull
.
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.)