Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CollectionView] Crash occurs when switching CollectionView.IsVisible right after setting ItemsSource #28194

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

kubaflo
Copy link
Contributor

@kubaflo kubaflo commented Mar 5, 2025

Description of Change

The error occurs because the children's collection is modified while iterating over it. To fix this, we should iterate over a snapshot of the collection instead of the collection itself. The crash happened only on iOS and only when ItemSource is ObservableCollection

Issues Fixed

Fixes #28162

<VerticalStackLayout>
    <Button Text="Toggle visibility"
            Clicked="Button_Clicked"/>
    <CollectionView IsVisible="True"
                    HorizontalOptions="Center"
                    VerticalOptions="Center"
                    x:Name="CollectionView">
        <CollectionView.Header>
            <ContentView HeightRequest="50"
                            BackgroundColor="Green"/>
        </CollectionView.Header>
        <CollectionView.ItemTemplate>
            <DataTemplate>
                <Border StrokeShape="RoundRectangle 10"
                        Padding="20"
                        Stroke="Red">
                    <Label HorizontalOptions="Center"
                            VerticalOptions="Center"
                            Text="{Binding .}"/>
                </Border>
            </DataTemplate>
        </CollectionView.ItemTemplate>
        <CollectionView.Footer>
            <ContentView HeightRequest="50"
                            BackgroundColor="Green"/>
        </CollectionView.Footer>
    </CollectionView>
</VerticalStackLayout>
private void Button_Clicked(object sender, EventArgs e)
{
	CollectionView.IsVisible = !CollectionView.IsVisible;
	CollectionView.ItemsSource = new ObservableCollection<string> { "Item 1", "Item 2", "Item 3" };
}

@dotnet-policy-service dotnet-policy-service bot added the community ✨ Community Contribution label Mar 5, 2025
Copy link
Contributor

Hey there @kubaflo! Thank you so much for your PR! Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

@PureWeen PureWeen added the p/0 Work that we can't release without label Mar 5, 2025
@PureWeen PureWeen added this to the .NET 9 SR5 milestone Mar 5, 2025
@kubaflo kubaflo marked this pull request as ready for review March 6, 2025 01:42
@kubaflo kubaflo requested a review from a team as a code owner March 6, 2025 01:42
@kubaflo kubaflo requested review from PureWeen and tj-devel709 March 6, 2025 01:42
@jsuarezruiz
Copy link
Contributor

/azp run

Copy link

Azure Pipelines successfully started running 3 pipeline(s).

Copy link
Contributor

@jsuarezruiz jsuarezruiz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we include a test using the sample from #28162?

@jsuarezruiz jsuarezruiz added the area-controls-collectionview CollectionView, CarouselView, IndicatorView label Mar 6, 2025
@bronteq
Copy link

bronteq commented Mar 6, 2025

Hi, a similar issue also happens on Windows platform: #20037 #18481

@rmarinho
Copy link
Member

rmarinho commented Mar 6, 2025

/rebase

Copy link

Azure Pipelines successfully started running 3 pipeline(s).

@PureWeen
Copy link
Member

PureWeen commented Mar 6, 2025

/azp run

Copy link

Azure Pipelines successfully started running 3 pipeline(s).

@PureWeen
Copy link
Member

PureWeen commented Mar 6, 2025

/azp run

Copy link

Azure Pipelines successfully started running 3 pipeline(s).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-controls-collectionview CollectionView, CarouselView, IndicatorView community ✨ Community Contribution p/0 Work that we can't release without
Projects
Status: Approved
Development

Successfully merging this pull request may close these issues.

[CollectionView]Crash occurs when switching CollectionView.IsVisible right after setting ItemsSource
7 participants