maui icon indicating copy to clipboard operation
maui copied to clipboard

MeasureFirstItem makes items disappear on Android

Open pekspro opened this issue 2 years ago • 13 comments

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

pekspro avatar May 28 '22 10:05 pekspro

can be reproduced on android 12 with above project.

kristinx0211 avatar May 30 '22 06:05 kristinx0211

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.

ToolmakerSteve avatar May 31 '22 03:05 ToolmakerSteve

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.

pekspro avatar Nov 11 '22 18:11 pekspro

Same issue here in .Net 7.0

  • collection view with measurefirst item on android
  • no issue except poor performance with measure all items

davidverriere avatar Feb 07 '23 08:02 davidverriere

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

symbiogenesis avatar Feb 08 '23 05:02 symbiogenesis

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

Android: image

XamlTest avatar May 10 '23 10:05 XamlTest

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

thomaskaelin avatar Jun 07 '23 12:06 thomaskaelin

I also noticed this :-(

Scordo avatar Jun 12 '23 17:06 Scordo

Bug remains in dotnet 8.0.0-preview.6.8686.

jvakos avatar Jul 19 '23 12:07 jvakos

Please fix this already!

giannisk79 avatar Jul 19 '23 12:07 giannisk79

No fix till this day still...

OliverDevsCodex avatar Aug 11 '23 19:08 OliverDevsCodex

Same exists on Dotnet 8 RC2

waseem852 avatar Oct 21 '23 10:10 waseem852

Still not working with .NET 8 GA. Setting "measure first item" making almost all items except first invisible. And performance with the default setting "measure all items" is terrible. Maybe there's some workaround via custom handlers?

andrew-barlit avatar Jan 25 '24 09:01 andrew-barlit

We also would appreciate a fix for this.

For iOS ist working and we see a approvment for scroll performance. Unfortunately this cannot be used for Android

Alex-111 avatar Jan 30 '24 21:01 Alex-111

I spent about an hour trying to understand why, on Android, we see only the first item from a collection to bump over this bug. This is very basic- showing a list of items with good performance on mobile apps. I am not sure how this can not be a P1 item to be fixed...

kyurkchyan avatar Feb 10 '24 11:02 kyurkchyan

I've had to replace Collection View with Vertical Stack Layout, since List View isn't working properly, too. While with collection view all items except first disappear if we set "measure first item", with list view they disappear randomly while scrolling and we don't even have "measure all" option. In my case there's about 10-50 items in the list, and 8-10 fits on the screen. Adding items manually from code-behind to stack layout (and doing a reordering for drag and drop) is reinventing the wheel, but this configuration is somehow way faster than collection view. All items are the same height, have height request set...

andrew-barlit avatar Feb 27 '24 19:02 andrew-barlit

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

Zerachiel01 avatar Mar 08 '24 22:03 Zerachiel01

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

PureWeen avatar Mar 27 '24 21:03 PureWeen

@PureWeen No still not working.

filipleifer avatar Apr 18 '24 11:04 filipleifer