Skip to content

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
@dotnet-policy-service
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 Current heighest priority issues that we are targeting for a release. 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

@azure-pipelines
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

@PureWeen PureWeen moved this from Todo to Changes Requested in MAUI SDK Ongoing Mar 6, 2025
@rmarinho
Copy link
Member

rmarinho commented Mar 6, 2025

/rebase

@azure-pipelines
Copy link

Azure Pipelines successfully started running 3 pipeline(s).

@PureWeen PureWeen moved this from Changes Requested to Ready To Review in MAUI SDK Ongoing Mar 6, 2025
@PureWeen
Copy link
Member

PureWeen commented Mar 6, 2025

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 3 pipeline(s).

@PureWeen
Copy link
Member

PureWeen commented Mar 6, 2025

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 3 pipeline(s).

@PureWeen PureWeen moved this from Ready To Review to Approved in MAUI SDK Ongoing Mar 6, 2025
@PureWeen PureWeen enabled auto-merge (squash) March 6, 2025 22:31
@PureWeen PureWeen disabled auto-merge March 7, 2025 13:18
@PureWeen PureWeen merged commit 47805ee into dotnet:main Mar 7, 2025
131 of 134 checks passed
@github-project-automation github-project-automation bot moved this from Approved to Done in MAUI SDK Ongoing Mar 7, 2025
bhavanesh2001 pushed a commit to bhavanesh2001/maui that referenced this pull request Mar 7, 2025
… right after setting ItemsSource (dotnet#28194)

* Update PropertyPropagationExtensions.cs

* Update PropertyPropagationExtensions.cs

* Added a UI Test

* Performance improvement

* Update PropertyPropagationExtensions.cs

* Update PropertyPropagationExtensions.cs

* Update PropertyPropagationExtensions.cs

* Use foreach

* Use to array
rmarinho pushed a commit that referenced this pull request Mar 11, 2025
… right after setting ItemsSource (#28194)

* Update PropertyPropagationExtensions.cs

* Update PropertyPropagationExtensions.cs

* Added a UI Test

* Performance improvement

* Update PropertyPropagationExtensions.cs

* Update PropertyPropagationExtensions.cs

* Update PropertyPropagationExtensions.cs

* Use foreach

* Use to array
@jdarwood007
Copy link

I am experiencing this issue as well. I see this has been merged. Which release should I expect to see this land in? I see the milestone, but I don't see information on what the milestone ties into releases I expect to see in visual studio.

@github-actions github-actions bot locked and limited conversation to collaborators Apr 18, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-controls-collectionview CollectionView, CarouselView, IndicatorView community ✨ Community Contribution p/0 Current heighest priority issues that we are targeting for a release.

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

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

8 participants