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

Merged
merged 9 commits into from
Mar 7, 2025

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).

@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
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.

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: Done
Development

Successfully merging this pull request may close these issues.

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