When using custom renderer for the content view. It throws "Element is already the child of another element" while converting the content of the content view to the content presenter using the content control.
Page should be navigated back properly with the content view as like the second time.
In first time page is not navigated back properly and also content view gets disappear.
Please check the below sample. CustomControl.zip
Can anyone help me with this?
Element is already the child of another element"
The problem is your CustomView
has been referenced by previous view, when you navigation back, the navigation create new ContentPage that want to use previous CustomView, but the previous CustomView has not be released. For solving this problem, you could set TitleViewProperty
as null when page OnDisappearing
.
protected override void OnDisappearing()
{
base.OnDisappearing();
SetValue(NavigationPage.TitleViewProperty, null);
}
Update
Please set TitleViewProperty
in OnAppearing method like the following.
protected override void OnAppearing()
{
base.OnAppearing();
SetValue(NavigationPage.TitleViewProperty, new NavigationView());
}
protected override void OnDisappearing()
{
base.OnDisappearing();
SetValue(NavigationPage.TitleViewProperty, null);
}