Hi,
I updated to the latest Xamarin.Forms version (4.7.0.1080) and now a list view I have in my project is totally messed up - the template of the list view is set to a custom view cell that it's View is set to a frame and now the content of the rows appear on top of each other.
I found this same problem in this sample - https://docs.microsoft.com/en-us/samples/xamarin/xamarin-forms-samples/boxview-listviewcolors (see screenshot)
Does anyone have a fix for this problem?
Thanks!
I updated to the latest Xamarin.Forms version (4.7.0.1080) and now a list view I have in my project is totally messed up
Did you try the code on other version like 4.7.0.968? I will test the sample of the link to check the issue later.
Update:
@Chasy_A I've tested the code of the posted link, it works fine on android but not on ios. The items are messed up in ios, this will
also occur in other versions. After testing, the issue is caused by the caching of the listView. To fix the issue, try to set the CachingStrategy to RecycleElementAndDataTemplate to improve the scrolling performance for the ListView
.
<ListView CachingStrategy="RecycleElementAndDataTemplate" ...> ... </ListView>
when I add the CachingStrategy I get an error: Property or indexer 'ListView.CachingStrategy' cannot be assigned to -- it is read only
The ListView
class doesn't provide the set method for the CachingStrategy property. However, it has the parameterized constructor which could help you to specify the caching category.
var listView = new ListView(ListViewCachingStrategy.RecycleElement);
Check the tutorials:
https://docs.microsoft.com/en-us/dotnet/api/xamarin.forms.listview.cachingstrategy?view=xamarin-forms#Xamarin_Forms_ListView_CachingStrategy
https://docs.microsoft.com/en-us/dotnet/api/xamarin.forms.listview.-ctor?view=xamarin-forms#Xamarin_Forms_ListView__ctor_Xamarin_Forms_ListViewCachingStrategy_
Answers
Did you try the code on other version like 4.7.0.968? I will test the sample of the link to check the issue later.
Update:
@Chasy_A I've tested the code of the posted link, it works fine on android but not on ios. The items are messed up in ios, this will
also occur in other versions. After testing, the issue is caused by the caching of the listView. To fix the issue, try to set the CachingStrategy to RecycleElementAndDataTemplate to improve the scrolling performance for the
ListView
.Thanks @Jarvan,
my code is written in C# and when I add the CachingStrategy I get an error: Property or indexer 'ListView.CachingStrategy' cannot be assigned to -- it is read only
The
ListView
class doesn't provide the set method for the CachingStrategy property. However, it has the parameterized constructor which could help you to specify the caching category.Check the tutorials:
https://docs.microsoft.com/en-us/dotnet/api/xamarin.forms.listview.cachingstrategy?view=xamarin-forms#Xamarin_Forms_ListView_CachingStrategy
https://docs.microsoft.com/en-us/dotnet/api/xamarin.forms.listview.-ctor?view=xamarin-forms#Xamarin_Forms_ListView__ctor_Xamarin_Forms_ListViewCachingStrategy_
Thank you @Jarvan!
I tried it but in my project it fixed only partially - I have a frame with a label in it that is added to the view cell OnBindingContextChanged and as soon as I start to scroll, the frames start to overlap with each other....
You can try it in the Xamrin's sample I linked - add to the ViewCell BindingContentChange:
This is because the collectionView reuse the cell repeatedly. To avoid this issue, don't add or remove a control to the cell's content. Instead, create a new view layout to replace the previous one. Or you could add the control to the cell at begining and set it invisible, change the control to be visible in the BindingContextChanged event.
@Jarvan , I tried your suggestion to recreate the view but it does not look good in my project,
In my project in the OnBinfingContextChange method I'm fetching data from a server based on data of the BindingContext and display it in a frame that I add to the existing view, if I create a new view or replace it the content of the cells jumps on the screen and each cell displays it content at a different second so the data order does not look good...
I marked you answer as accepted because basically it does help...
Thanks!