maui icon indicating copy to clipboard operation
maui copied to clipboard

`CollectionView` throws `NRE` when `ItemTemplate` uses a `ViewCell` as parent

Open AndreasReitberger opened this issue 1 year ago • 4 comments

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

  1. Fork the repro
  2. Run it on windows (not tested any other platforms)
  3. See that an exception is thrown
  4. Replace ViewCell with a Grid and 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

AndreasReitberger avatar Aug 22 '24 13:08 AndreasReitberger

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:

Note: You can give me feedback by thumbs upping or thumbs downing this comment.

github-actions[bot] avatar Aug 22 '24 13:08 github-actions[bot]

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. image 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. image

ninachen03 avatar Aug 23 '24 04:08 ninachen03

ViewCells aren't a compatible type with CollectionView they are really only a ListView concept

We should look at

  1. Maybe they should be, they do have some set of useful properties for conveying size information that could help with performance.
  2. throwing a better exception message

Is there any reason why you are wanting to use a ViewCell? What use case does a viewCell provide?

PureWeen avatar Aug 23 '24 18:08 PureWeen

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!

AndreasReitberger avatar Aug 26 '24 07:08 AndreasReitberger