maui
maui copied to clipboard
Binding `Content` Property of `ContentView` to a nested property would result in re-parenting error on Android
Description
Checkout the reproduction repository for sample code. But basically if you bind the content
to something like Object.Property
and have Object
changed to something else, upon re-rending the ContentView
you will get
Java.Lang.IllegalStateException
Message=The specified child already has a parent. You must call removeView() on the child's parent first.
Steps to Reproduce
- Checkout the code from repo (branch:
reparenting-issue
) - Run the app in Android emulator
- Click on
Clicked x times
button 3 times - The app crashes immediately upon 3rd click
Link to public reproduction project repository
https://github.com/rbakhshi/bugreporting/tree/reparenting-issue
Version with bug
7.0 (current)
Last version that worked well
Unknown/Other
Affected platforms
Android, I was not able test on other platforms
Affected platform versions
Android Api 32
Did you find any workaround?
In MainPage.Xaml
if you comment the block from line 12-14 and instead uncomment the block below it (from line 16-21) it will work.
Basically avoid navigational property access in binding of content and instead bind to each property one at a time:
This fails:
<ControlTemplate x:Key="DynamicContent">
<ContentView Content="{Binding BindingContext.ContentHeader, Source={RelativeSource AncestorType={x:Type ContentPage}}}"/>
</ControlTemplate>
This works:
<ControlTemplate x:Key="DynamicContent">
<ContentView BindingContext="{Binding BindingContext, Source={RelativeSource AncestorType={x:Type ContentPage}}}">
<ContentView Content="{Binding ContentHeader}"/>
</ContentView>
</ControlTemplate>
Relevant log output
No response