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.28k stars 1.76k forks source link

CollectionViewHandler2 null reference exception if ItemsLayout is set for Tablet but NOT Phone #26065

Open rysbilinski opened 1 week ago

rysbilinski commented 1 week ago

Description

Adding a collectionview to a page and only setting the ItemsLayout property for Tablet will throw a null reference exception before the page appears on mobile.

Steps to Reproduce

  1. Add A collectionview to a page, and add the following to the collection view:

<CollectionView ItemsLayout="{OnIdiom Tablet='VerticalGrid 2'}" ItemsSource="{Binding Items}" VerticalOptions="Fill">

  1. Configure builder to use the new CollectionViewHandler2:
builder.ConfigureMauiHandlers(handlers =>
                {

                    handlers.AddHandler<Microsoft.Maui.Controls.CollectionView, Microsoft.Maui.Controls.Handlers.Items2.CollectionViewHandler2>();

                    handlers.AddHandler<Microsoft.Maui.Controls.CarouselView, Microsoft.Maui.Controls.Handlers.Items2.CarouselViewHandler2>();

                });
  1. Run the application on iOS
  2. Navigate to the page and you will see a crash in Program.cs

System.NullReferenceException: 'Object reference not set to an instance of an object.'

Link to public reproduction project repository

https://github.com/rysbilinski/CollectionViewHandler2Bugs

Version with bug

9.0.10 SR1

Is this a regression from previous behavior?

Yes, this used to work in .NET MAUI

Last version that worked well

8.0.100 SR10

Affected platforms

iOS

Affected platform versions

No response

Did you find any workaround?

Workaround was to add the "phone" OnIdiom parameter:

<CollectionView ItemsLayout="{OnIdiom Phone='VeritcalGrid', Tablet='VerticalGrid 2'}" ItemsSource="{Binding Items}" VerticalOptions="Fill">

Relevant log output

ninachen03 commented 6 days ago

I can repro this issue at iOS platform on the latest 17.13.0 Preview 1.0(9.0.10 & 9.0.0-preview.7.24407.4)The CVhandler2 was introduced in .NET 9.0, so it cannot be verified with NET8. I will keep the potential-regression lab for now, because users say it works on 8.0.100, which is confusing information to me. If you have any question, please feel free to contact us. Image