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.21k stars 1.75k forks source link

[iOS] Items added to ObservableCollection do not appear in a bound stack layout inside a scroll view #23439

Open smegleyshire2 opened 4 months ago

smegleyshire2 commented 4 months ago

Description

I have linked to a small demo app that shows the issue. When run you see the following (Android):

image

Tap ADD ITEM a few times and a label for each item appears under the buttons:

image

The items are in a scroll view which starts scrolling when the view reaches a height of 350. Tap REMOVE ITEM to remove items from the bottom.

Now try running the demo app on iOS. No items appear but they are being created and you can see them in the visual tree inspector in Visual Studio.

XAML:

<?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="BugDemoApp.MainPage">

<Grid RowDefinitions="Auto, *">
    <VerticalStackLayout>
        <HorizontalStackLayout Spacing="4" HorizontalOptions="Center">
            <Button Text="ADD ITEM" Clicked="btnAdd_Clicked"/>
            <Button Text="REMOVE ITEM" Clicked="btnRemove_Clicked" />
        </HorizontalStackLayout>
        <ScrollView MaximumHeightRequest="350">
            <VerticalStackLayout x:Name="slItems">
                <BindableLayout.ItemTemplate>
                    <DataTemplate>
                        <Label Text="{Binding .}"/>
                    </DataTemplate>
                </BindableLayout.ItemTemplate>
            </VerticalStackLayout>
        </ScrollView>
    </VerticalStackLayout>
    <Grid Grid.Row="1" BackgroundColor="Red"/>
</Grid>

Code:

using System.Collections.ObjectModel;

namespace BugDemoApp { public partial class MainPage : ContentPage { private ObservableCollection Items = new ObservableCollection();

    public MainPage()
    {
        InitializeComponent();

        BindableLayout.SetItemsSource(slItems, Items);
    }

    private void btnAdd_Clicked(object sender, EventArgs e)
    {
        Items.Add($"Item {Items.Count + 1}");
    }

    private void btnRemove_Clicked(object sender, EventArgs e)
    {
        if (Items.Count > 0)
            Items.RemoveAt(Items.Count - 1);
    }
}

}

Steps to Reproduce

See description above

Link to public reproduction project repository

https://github.com/smegleyshire2/MauiBugDemoApp

Version with bug

8.0.61 SR6.1

Is this a regression from previous behavior?

Yes, this used to work in Xamarin.Forms

Last version that worked well

Unknown/Other

Affected platforms

iOS

Affected platform versions

iOS 17.5

Did you find any workaround?

When ADD ITEM is tapped, I have to change the size of something on the page to force a re-layout. This causes the correct number of items to be displayed.

The buttons in the demo app are in a horizontal stack layout which is centred horizontally. I've found that tweaking the horizontal margin of that horizontal stack layout each time an item is added forces the re-layout to occur without moving the buttons (as its horizontally centred). So, if the horizontal stack layout is given a name:

x:Name="slButtons"

then doing this on each tap causes a re-layout and the items to update on iOS:

slButtons.Margin = (slButtons.Margin.Left == 0) ? new Thickness(1,0) : new Thickness(0);

Relevant log output

No response

github-actions[bot] commented 4 months ago

Hi I'm an AI powered bot that finds similar issues based off the issue title.

Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one and thumbs upping the other issue to help us prioritize it. Thank you!

Open similar issues:

Closed similar issues:

Note: You can give me feedback by thumbs upping or thumbs downing this comment.

Zhanglirong-Winnie commented 4 months ago

This issue has been verified using Visual Studio 17.11.0 Preview 2.1(8.0.61 & 8.0.40). Can repro on iOS platform.

smegleyshire2 commented 4 months ago

Thanks Winnie!