Skip to content

MeasureFirstItem makes items disappear on Android #7562

Closed

Description

@pekspro

Description

I have a CollectionView where ItemSizingStrategy is set to MeasureFirstItem. The properties of the times are changed some time of the CollectionView is loaded. Somehow, this makes some items become invisible on Android. It works fine on Windows.

Originally, my list looks like this:

image

Then, the two first items should be changed to green. But the second one just disappears:

image

In Windows it's works fine:

image

It's very strange :-)

Steps to Reproduce

  1. Create a new Maui app.
  2. Change MainPage.xaml to:
<?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"
             xmlns:v="clr-namespace:MauiIssues"
             x:Class="MauiIssues.MainPage"
             >
    <Grid>
        <CollectionView ItemsSource="{Binding Issues}" ItemSizingStrategy="MeasureFirstItem">
            <CollectionView.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Label Text="{Binding Title, Mode=OneWay}"
                            TextColor="Red"
                            IsVisible="{Binding IsRed}"
                                />

                        <Label Text="{Binding Title, Mode=OneWay}"
                            TextColor="Green"
                            IsVisible="{Binding IsGreen}}"
                                />
                    </Grid>
                </DataTemplate>
            </CollectionView.ItemTemplate>
        </CollectionView>
    </Grid>
</ContentPage>
  1. Change MainPage.cs to:
using System.Collections.ObjectModel;

namespace MauiIssues;

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();

        BindingContext = this;
    }

    public class Issue : BindableObject
    {
        public string Title { get; set; }
        public bool IsRed { get; set; } = true;
        public bool IsGreen => !IsRed;

        public void MakeGreen()
        {
            IsRed = false;
            OnPropertyChanged(nameof(IsGreen));
            OnPropertyChanged(nameof(IsRed));
        }
    }

    public ObservableCollection<Issue> Issues { get; } = new ObservableCollection<Issue>();

    protected async override void OnAppearing()
    {
        base.OnAppearing();

        Issues.Add(new Issue { Title = "Issue 1" });
        Issues.Add(new Issue { Title = "Issue 2" });
        Issues.Add(new Issue { Title = "Issue 3" });

        await Task.Delay(4000);

        Issues[0].MakeGreen();
        Issues[1].MakeGreen();
    }
}

Run the project in Android.

Full sample code: https://github.com/pekspro/MauiIssues/tree/7562_MeasureFirstItem_makes_items_disappear

Version with bug

6.0 (current)

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

Windows 10.0.17763.0, Android 11

Did you find any workaround?

Removing ItemSizingStrategy="MeasureFirstItem" solves the problem.

Relevant log output

No response

Activity

  1. added
    s/needs-verification
    t/bug
    on May 28, 2022
  2. added
    s/verified
    and removed
    s/needs-verification
    on May 30, 2022
  3. kristinx0211 commented on May 30, 2022

    @kristinx0211

    can be reproduced on android 12 with above project.

  4. ToolmakerSteve commented on May 31, 2022

    @ToolmakerSteve

    Certain UI changes in OnAppearing have always been unreliable in Xamarin.Forms. Perhaps the improvements for Maui have not helped avoid this "twilight zone".

    Here, its those OnPropertyChanged calls that can't be relied on to take effect.

    It would be great to see this kind of issue fixed.


    FWIW (work-around):

    The 4 second delay shown there is useless; all that does is suspend maui's processing of the page. If you instead do

    Dispatcher.DispatchDelayed(TimeSpan.FromSeconds(0.5), () =>
    {
        Issues[0].MakeGreen();
        Issues[1].MakeGreen();
    });
    

    I'm sure it will work.

  5. added
    area-controls-collectionview
    on May 31, 2022
  6. pekspro commented on Nov 11, 2022

    @pekspro
    Author

    I've updated sample application to .NET 7. The same issue remains. I have also tried the work around from @ToolmakerSteve, but it didn't helped.

  7. davidverriere commented on Feb 7, 2023

    @davidverriere

    Same issue here in .Net 7.0

    • collection view with measurefirst item on android
    • no issue except poor performance with measure all items
  8. symbiogenesis commented on Feb 8, 2023

    @symbiogenesis
    Contributor

    I'm seeing the same on Android, only. Using .NET 7 with the latest workloads.

  9. removed
    s/verified
    on Apr 5, 2023
  10. added
    s/verified
    s/triaged
    on May 10, 2023
  11. XamlTest commented on May 10, 2023

    @XamlTest

    Verified this on Visual Studio Enterprise 17.6.0 Preview 7.0. Repro on Android 13.0, with below Project:
    7562.zip

    Android:
    image

  12. thomaskaelin commented on Jun 7, 2023

    @thomaskaelin

    I also noticed this issue while migrating an Xamarin.Forms-app to .NET MAUI. Any news on this?

  13. 19 remaining items

  14. added this to the .NET 8 SR4 milestone on Mar 5, 2024
  15. Zerachiel01 commented on Mar 8, 2024

    @Zerachiel01

    Please fix this. I'm even thinking of leaving MAUI and choosing another framework because of this and the performance loss without it.

  16. self-assigned this
    on Mar 22, 2024
  17. PureWeen commented on Mar 27, 2024

    @PureWeen
    Member

    This should be fixed our nightlies

    If anyone can test and let me know your results that would be super!
    Can you test with the latest nightly build?
    https://github.com/dotnet/maui/wiki/Nightly-Builds

  18. filipleifer commented on Apr 18, 2024

    @filipleifer

    @PureWeen No still not working.

  19. locked and limited conversation to collaborators on Jun 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-controls-collectionviewdelighterdelighter-scfixed-in-8.0.20fixed-in-9.0.0-preview.3.10457p/1platform/androids/triageds/verifiedt/bug

Type

No type

Projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions