maui
maui copied to clipboard
`CollectionView` throws `NRE` when `ItemTemplate` uses a `ViewCell` as parent
Description
If using a ViewCell in the DataTemplate, the application throws an exception (NRE).
<CollectionView
ItemsSource="{Binding Events}"
SelectionMode="None"
>
<CollectionView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Label
LineBreakMode="WordWrap"
Style="{StaticResource SubHeadline}"
Text="{Binding .}"
FontAttributes="Bold"
VerticalTextAlignment="Center"
/>
</ViewCell>
</DataTemplate>
</CollectionView.ItemTemplate>
<CollectionView.EmptyView>
<Label
Text="No items..."
VerticalTextAlignment="Center"
HorizontalTextAlignment="Center"
/>
</CollectionView.EmptyView>
</CollectionView>
Steps to Reproduce
- Fork the repro
- Run it on windows (not tested any other platforms)
- See that an exception is thrown
- Replace
ViewCellwith aGridand see that it is working afterwards
Link to public reproduction project repository
https://github.com/AndreasReitberger/MauiCollectionViewBug
Version with bug
8.0.80 SR8
Is this a regression from previous behavior?
Not sure, did not test other versions
Last version that worked well
Unknown/Other
Affected platforms
Windows, I was not able test on other platforms
Affected platform versions
No response
Did you find any workaround?
Replace the ViewCell with a Grid or something similar.
Relevant log output
No response
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!
Closed similar issues:
- [regression/8.0.0-preview.6.8686] [WinUI] CollectionView throws an exception after adding a second item (#16320), similarity score: 0.76
- [iOS] NullReferenceException in TemplatedCell (#20606), similarity score: 0.76
- Changing
<DataTemplate/>contents can give a runtime exception (#11203), similarity score: 0.76 - Android - CollectionView DataTemplate with ContentView Content binding causes error when scrolling (#20560), similarity score: 0.76
- [Bug] DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION when adding a CollectionView (#1561), similarity score: 0.75
Note: You can give me feedback by thumbs upping or thumbs downing this comment.
This issue has been verified using Visual Studio 17.12.0 Preview 1.0 (8.0.80 & 8.0.3). Can repro on Windows platform.
NET 7 have "CS9058 Feature 'collection expressions'' is not available in C# 11.0. Please use language version 12.0 or greater" so we will temporarily skip the verification of 7.
ViewCells aren't a compatible type with CollectionView they are really only a ListView concept
We should look at
- Maybe they should be, they do have some set of useful properties for conveying size information that could help with performance.
- throwing a better exception message
Is there any reason why you are wanting to use a ViewCell? What use case does a viewCell provide?
Hi @PureWeen
thanks for your reply. This is an ItemTemplate which was used in a ListView first. Then I decided to switch to a CollectionView then. As a workaround I'll update my shared ItemTemplates to do not use a ViewCell as parent control.
There is no special reason to stick to the ViewCell for me though. And yes, a better exception would be great :)
Thanks!