xamarin / Xamarin.Forms

Xamarin.Forms is no longer supported. Migrate your apps to .NET MAUI.
https://aka.ms/xamarin-upgrade
Other
5.62k stars 1.87k forks source link

[Bug] CollectionView iOS draws both EmptyView and Items/ItemTemplate #13794

Closed RemcoDEV closed 2 years ago

RemcoDEV commented 3 years ago

Description

CollectionView iOS draws both EmptyView and Items/ItemTemplate. The itemtemplate UI becomes unresponsive, because the emptyview is drawn over it.

Steps to Reproduce

  1. Create a collectionview with an itemtemplate and an emptyview.
  2. Fill the collectionview in the viewmodel and call onpropertychanged on the itemssource binding.
  3. Sometimes (~3 out of 5 times) the collectionview displays both the emptyview and the itemtemplates.

Expected Behavior

Only draw the emptyview when the itemssource is empty.

Actual Behavior

Both the emptyview and the items/itemtemplates are drawn.

Basic Information

Environment

Visual Studio 2019 / Windows 10

Build Logs

--

Screenshots

'Nothing to display' is the emptyview. This example is a grouped collectionview, but it also happens to non-grouped collectionviews. IMG_0060

Reproduction Link

--

Workaround

Reconstructing the page sometimes works. Refreshing does not work. Something definitely goes wrong when constructing the page.

hartez commented 3 years ago

@RemcoDEV Can you post a small reproduction project? Or at least paste in an example of your ItemTemplate, EmptyView, and the code you're using to set up your ItemsSource?

lewinzki commented 3 years ago

@RemcoDEV I encounter the same bug. A possible workaround is to add IsVisible to the content of your EmptyView. E.g. <StackLayout IsVisible="{Binding DocumentGroups, Converter={StaticResource ListIsNullOrEmptyConverter}}">. Here I have used the converter ListIsNullOrEmptyConverter from the XamarinCommunityToolkit. The converter is also easy to implement yourself if you don't want the dependency.

@hartez If it is any help I can report that this bug only occurs when the CollectionView is a child of a TabbedPage.

decv86 commented 3 years ago

@RemcoDEV I encounter the same bug. A possible workaround is to add IsVisible to the content of your EmptyView. E.g. <StackLayout IsVisible="{Binding DocumentGroups, Converter={StaticResource ListIsNullOrEmptyConverter}}">. Here I have used the converter ListIsNullOrEmptyConverter from the XamarinCommunityToolkit. The converter is also easy to implement yourself if you don't want the dependency.

@hartez If it is any help I can report that this bug only occurs when the CollectionView is a child of a TabbedPage.

Tnx man!

Deepfreezed commented 3 years ago

Same issue for me. Draws both Empty and Item View. Not using Tabbed Page. Xamarin test team should really use rest service to test with some async calls. I am sure all this works when you have data hardcoded into the view model.

Just updated to 5.0.0.2012 from 5.0.0.1931. There are other issue in 5.0.0.1931 that breaks CollectionView binding when visibility state is initially false. At least that seems to be fixed in 5.0.0.2012.

rmarinho commented 3 years ago

Hello all, can someone please upload a small reproduction of this issue so we can investigate it further?!

thanks.

marcojak commented 3 years ago

Same problem here.

Redth commented 3 years ago

@marcojak can you please upload a small reproduction of the issue?

beeradmoore commented 3 years ago

@hartez @rmarinho @Redth , I have added a small repro project. There is some gross code incoming so brace yourselves.

Simulator Screen Shot - iPhone 12 mini - 2021-05-07 at 11 30 52

In this particular case I was able to replicate it by doing what I do in my prod app, which took me down a rabbit hold where I found some jank code. Fixing my jank fixes the issue, however I think the underlying issue may still happen in other circumstances.

Problem is only present when for me CollectionView is inside a CarouselView. If I replace the CarouselView with a CollectionView the problem goes away. The other bit of jank in the code is I am creating an ObservableCollection on my MainPage which is populated by loading a local file/web request/etc, and a reference of this is passed to MyCollectionView class.

Within MyCollectionView I keep a reference of the ObservableCollection (Data = data;) AFTER I set the BindingContext.

My workaround is to fix the jank and move this line to before I set the BindingContext and then the problem goes away. Another possible solution I havn't tried would be setting ItemsSource of MyCollectionView from the MainPage.

EmptyCollectionViewBug.zip

So yeah, there is some things here that I shouldn't be doing, but its still putting CollectionView in an odd state where it both has and does not have data.

jfversluis commented 3 years ago

@RemcoDEV @beeradmore, a PR for this is open now (#14603), would you be able to grab the NuGet as described here and let us know if this fixes this issue? That will greatly speed up the review process.

Besides verifying if this particular issue is fixed also be sure to check other scenarios in the same area to make sure that this fix doesn't accidentally has side-effects 🙂

Thanks!

RemcoDEV commented 3 years ago

Hi @jfversluis ,

Just tested. It still does not work on the latest XF 5.0.0.2125 (pull request of Javier is from before that release date). The pull request of Javier does not change any logic, it are just tests. It can be approved without causing any side effects :).

image

Regards, Remco

jfversluis commented 3 years ago

Sorry about that Remco! Just noticed a PR and added that message in the hope you could test it :D

Seems we have some work to do here still, thanks!

jfversluis commented 2 years ago

I suspect something in here has to do with it: https://github.com/xamarin/Xamarin.Forms/pull/13678

jfversluis commented 2 years ago

Hey @beeradmoore @RemcoDEV, a PR (#15122) for this is open now, would you be able to grab the NuGet as described here and let us know if this fixes this issue? That will greatly speed up the review process. Make sure to take the exact version as attached to this PR.

Besides verifying if this particular issue is fixed also be sure to check other scenarios in the same area to make sure that this fix doesn't accidentally has side-effects 🙂

Thanks!

RemcoDEV commented 2 years ago

Hi @jfversluis . It seems to work as intended. No side effects found. Thank you!

beeradmoore commented 2 years ago

@jfversluis , my above repro is in a weird state where trying to use it (either with the XF it has, updated, or updated to the PR version) where attempting to scroll it crashes it. That repro also has some weird things happening in it so it may be because of that.

I tried it in our production app and I didn't notice any new problems. However in its pre-PR-nuget state we also no longer were experiencing this issue.

I however do still have header/footer showing when CollectionView is empty and displaying the correct EmptyView. I have a feeling that this isn't a bug and instead a design choice. Changing that behaviour will impact anyone using header/footer + EmptyView so its probably best left untouched (unless we want to add some new HideHeaderOnEmpty HideFooterOnEmpty properties to CollectionView in MAUI).

jfversluis commented 2 years ago

Perfect, thanks so much for taking the time to verify and provide me with this input! Looks like I'll be merging this for now and look at the other mentioned things separately.