maui icon indicating copy to clipboard operation
maui copied to clipboard

Binding `Content` Property of `ContentView` to a nested property would result in re-parenting error on Android

Open rbakhshi opened this issue 2 years ago • 0 comments

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

  1. Checkout the code from repo (branch: reparenting-issue)
  2. Run the app in Android emulator
  3. Click on Clicked x times button 3 times
  4. 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

rbakhshi avatar Dec 22 '22 03:12 rbakhshi