xamarin / Xamarin.Forms

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

[Bug] CollectionView not getting renderer in UWP and iOS #15087

Open SujaVenkatesan opened 2 years ago

SujaVenkatesan commented 2 years ago

Description

In a XF iOS and UWP the collection not getting renderer if we set the collectionview as content to Frame or contentview in secondtime . It works fine in Xamarin.Forms.Android platform.

Steps to Reproduce

  1. Run the reproduction sample in Xamarin.Forms.iOS or Xamarin.Forms.UWP
  2. Click open_withCollectionView button.
  3. CollectionView gets renderer below the buttons.
  4. Then, Click the ClosePopup button, collection gets cleared.
  5. Once again click the open_withCollectionView button.
  6. Now Collectionview not getting renderer in Xamariin.Forms.iOS and Xamarin.Forms.UWP, but it gets renderer properly in Xamarin.Forms.Android.

Expected Behavior

Need to collectionview gets renderer for everytime after clicking the open_collectionView button

Actual Behavior

Collectionview not getting renderer in Xamariin.Forms.iOS and Xamarin.Forms.UWP, but it gets renderer properly in Xamarin.Forms.Android.

Basic Information

Screenshots

demo.zip

Reproduction Link

SfPopupLayout.zip

jtorvald commented 2 years ago

I've a similar experience with CollectionView on iOS. When I navigate to an empty page in Shell (ShellContent with ContentTemplate but outside of the current tabs/section) and then I navigate back by opening menu, pick the first menu item with the tabs the CollectionView shows but not the items in it. When I navigate within the the bottom tabs or top tabs of Shell, the CollectionView items stay visible.

When Xamarin HotReload triggers a reload of the page, the content appears again. I tried, Measure, or to send a scroll event/change background color but that does trigger the items to be shown.

I did not manage to reproduce this issue in an isolated project so I can not say if it's exactly the same issue but it feels like it.

I managed to workaround this bug by setting the ItemSource again in the OnAppearing. That loads the items again. I noticed that with this workaround; if I switch tabs and get back to this page, the scroll index is still the same, but when I come back from the other page it gets reset to the first element (which I was expecting to do on every OnAppearing). Not sure if it's relevant but I though to mention that in case it might give a clue.

jtorvald commented 2 years ago

I tried to hunt down the bug in Xamarin.Forms development branch but had problems with setting up the development environment with my project. I've not been able to reference the development project and to debug. Maybe someone that reads this know how to do that, but I kept jumping into the .dll from an older nuget package.

Anyway, what I found is that a simple custom renderer fixes this issue for iOS in my case. So I don't longer need to set the ItemsSource in the OnAppearing as my previous workaround suggested.

using MyProject.iOS.Renderers;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;

[assembly: ExportRenderer(typeof(CollectionView), typeof(MyCollectionViewRenderer))]
namespace MyProject.iOS.Renderers
{
    public class MyCollectionViewRenderer : CollectionViewRenderer
    {

    }
}

edit: this doesn't solve your problem, so I'm looking into your solution now. That looks like a complicated mix of XAML and code behind, no wonder you get confused.

I think what you wanted to do was:

        private void Button_Clicked_1(object sender, EventArgs e)
        {
            // above you set the view, now you probably want to undo that.
            PopupContent = null;

            // here you remove all the content from the Grid in the XAML, including the frame content with the binding. 
            //popupview.Content = null;
        }

The above fix makes it work but I would seriously recommend to find another solution.