dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22.24k stars 1.76k forks source link

CarouselView - The specified child already has a parent. You must call removeView() on the child's parent first #16577

Closed ericbrunner closed 10 months ago

ericbrunner commented 1 year ago

Description

I am trying to setup a CarouselView in my ContentPage whereby the content items are defined as ContentView items (which have there layout and content completely defined in their own XAML file). Like so:

<CarouselView Grid.Row="1"
                      x:Name="CarouselView"
                      ItemsSource="{Binding Source={x:Reference This}, Path=TabItems}">
            <CarouselView.ItemTemplate>
                <DataTemplate>
                    <Grid x:DataType="models:ITabItem">
                        <ContentView Grid.Row="0" Content="{Binding View}" />
                    </Grid>
                </DataTemplate>
            </CarouselView.ItemTemplate>
</CarouselView>

The TabItems is an observable collection like:

            public ObservableCollection<ITabItem> TabItems   
    {
        get => (ObservableCollection<ITabItem>)GetValue(TabItemsProperty);
        set => SetValue(TabItemsProperty, value);
    }

Installed workloads: image

Steps to Reproduce

  1. Clone provided repo https://github.com/ericbrunner/TaskyApp.Maui.SingleProject.git.
  2. Click on image
  3. App crashes like image

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

Is there a way in .NET MAUI to show custom controls based on ContentView class to a CarouselView?

Link to public reproduction project repository

https://github.com/ericbrunner/TaskyApp.Maui.SingleProject/commit/832289f9f126f5189552d0e9ab50b1c7d57138fd

Version with bug

Unknown/Other

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

Android 13

Did you find any workaround?

No. A further attempt would be to create a DataTemplatefor each individual custom Control (ContentView) and use a DataTemplateSelector

Relevant log output

[mono-rt] [ERROR] FATAL UNHANDLED EXCEPTION: Java.Lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
[mono-rt]    at Java.Interop.JniEnvironment.InstanceMethods.CallNonvirtualVoidMethod(JniObjectReference instance, JniObjectReference type, JniMethodInfo method, JniArgumentValue* args) in /Users/runner/work/1/s/xamarin-android/external/Java.Interop/src/Java.Interop/obj/Release/net7.0/JniEnvironment.g.cs:line 12324
[mono-rt]    at Java.Interop.JniPeerMembers.JniInstanceMethods.InvokeVirtualVoidMethod(String encodedMember, IJavaPeerable self, JniArgumentValue* parameters) in /Users/runner/work/1/s/xamarin-android/external/Java.Interop/src/Java.Interop/Java.Interop/JniPeerMembers.JniInstanceMethods_Invoke.cs:line 75
[mono-rt]    at Android.Views.ViewGroup.AddView(View child) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/obj/Release/net7.0/android-33/mcw/Android.Views.ViewGroup.cs:line 1999
[mono-rt]    at Microsoft.Maui.Handlers.ContentViewHandler.UpdateContent(IContentViewHandler handler) in D:\a\_work\1\s\src\Core\src\Handlers\ContentView\ContentViewHandler.Android.cs:line 44
[mono-rt]    at Microsoft.Maui.Handlers.ContentViewHandler.MapContent(IContentViewHandler handler, IContentView page) in D:\a\_work\1\s\src\Core\src\Handlers\ContentView\ContentViewHandler.Android.cs:line 49
[mono-rt]    at Microsoft.Maui.PropertyMapper`2.<>c__DisplayClass5_0[[Microsoft.Maui.IContentView, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Handlers.IContentViewHandler, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].<Add>b__0(IElementHandler h, IElement v) in D:\a\_work\1\s\src\Core\src\PropertyMapper.cs:line 172
ghost commented 1 year ago

We've added this issue to our backlog, and we will work to address it as time and resources allow. If you have any additional information or questions about this issue, please leave a comment. For additional info about issue management, please read our Triage Process.

VladislavAntonyuk commented 1 year ago

the issue still exists in .NET 8 RC1

ghost commented 10 months ago

Hi @ericbrunner. We have added the "s/try-latest-version" label to this issue, which indicates that we'd like you to try and reproduce this issue on the latest available public version. This can happen because we think that this issue was fixed in a version that has just been released, or the information provided by you indicates that you might be working with an older version.

You can install the latest version by installing the latest Visual Studio (Preview) with the .NET MAUI workload installed. If the issue still persists, please let us know with any additional details and ideally a reproduction project provided through a GitHub repository.

This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

homeyf commented 10 months ago

Verified this issue with Visual Studio Enterprise 17.9.0 Preview 2. Not repro on android platform with sample project. https://github.com/ericbrunner/TaskyApp.Maui.SingleProject/commit/832289f9f126f5189552d0e9ab50b1c7d57138fd image