I have a control in a page that's pushed on the navigation stack. After the page is popped, the element's renderer's OnElementChanged is fired.
Why is it doing this whilst the element is being removed? Why is it rendering a component that's invisible?
It wouldn't be an issue apart from the fact that some element configuration code crashes.
When a Custom Renderer starts its execution, the first method that gets fired is OnElementChanged. In this method is where you can access the properties and perform any customizations.
To note, this method consumes an important parameter called ElementChangedEventArgs with two properties.
NewElementOldElementNewElement contains a reference to the Xamarin Forms control. OldElement contains a reference to the renderer that the Xamarin Forms control was attached to. It is important to pay attention to the two properties in subscribing and unsubscribing of events to avoid memory leaks.
protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
{
base.OnElementChanged(e);
if (e.NewElement != null)
{
// Subscribe for events
e.NewElement.SizeChanged += XFButtonOnSizeChanged;
}
else if (e.OldElement != null)
{
// Unsubscribe from events
e.OldElement.SizeChanged -= XFButtonOnSizeChanged;
}
}
P.S.: Usually I share a link to the official documentation, however in this particular case the realm academy has more detailed explanation in my opinion.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With