maui
maui copied to clipboard
MeasureFirstItem makes items disappear on Android
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:
Then, the two first items should be changed to green. But the second one just disappears:
In Windows it's works fine:
It's very strange :-)
Steps to Reproduce
- Create a new Maui app.
- 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>
- 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
can be reproduced on android 12 with above project.
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.
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.
Same issue here in .Net 7.0
- collection view with measurefirst item on android
- no issue except poor performance with measure all items
I'm seeing the same on Android, only. Using .NET 7 with the latest workloads.
Verified this on Visual Studio Enterprise 17.6.0 Preview 7.0. Repro on Android 13.0, with below Project: 7562.zip
Android:
I also noticed this issue while migrating an Xamarin.Forms-app to .NET MAUI. Any news on this?
I also noticed this :-(
Bug remains in dotnet 8.0.0-preview.6.8686.
Please fix this already!
No fix till this day still...
Same exists on Dotnet 8 RC2
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?
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
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...
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...
Please fix this. I'm even thinking of leaving MAUI and choosing another framework because of this and the performance loss without it.
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 No still not working.