Home > other >  CarouselView The specified child already has a parent
CarouselView The specified child already has a parent

Time:10-27

When I run the following code I get the following error message:

Java.Lang.IllegalStateException: 'The specified child already has a parent. You must call removeView() on the child's parent first.'

<CarouselView>
 <CarouselView.ItemsSource>
  <x:Array Type="{x:Type ContentView}">
   <ContentView></ContentView>
   <ContentView></ContentView>
   <ContentView></ContentView>
  </x:Array>
 </CarouselView.ItemsSource>
  <CarouselView.ItemTemplate>
   <DataTemplate>
     <ContentPresenter Content="{Binding .}"/>
   </DataTemplate>
  </CarouselView.ItemTemplate>
</CarouselView>

I don't know if you are allowed to set the ItemsSource of the CarouselView like this, but I get the same error message when I try it with a binding.

CodePudding user response:

According to this maui issue, to use Maui ContentView's directly like this, must set CarouselView's Loop property to false:

<CarouselView Loop="False">

CONSEQUENCE: the views won't form a "loop": can't swipe from last view back to first view.

If you need "looping" behavior, the alternative is to not define ItemsSource in xaml. Instead, have an ItemsSource that does NOT contain UI classes, with a DataTemplate or DataTemplateSelector that defines (or references) the desired UI class, based on each item. [Item is a "model"; template uses bindings to represent that model via UI elements.]

  • Related