Notice (2018-05-24): bugzilla.xamarin.com is now in
Please join us on
Visual Studio Developer Community and in the
Mono organizations on
GitHub to continue tracking issues. Bugzilla will remain
available for reference in read-only mode. We will continue to work
on open Bugzilla bugs, copy them to the new locations
as needed for follow-up, and add the new items under Related
Our sincere thanks to everyone who has contributed on this bug
tracker over the years. Thanks also for your understanding as we
make these adjustments and improvements for the future.
Please create a new report on
Developer Community or GitHub with
your current version information, steps to reproduce, and relevant error
messages or log files if you are hitting an issue that looks similar to
this resolved bug and you do not yet see a matching new report.
I implemented a custom view cell for the ListView DataTemplate as described here: https://developer.xamarin.com/guides/xamarin-forms/custom-renderer/viewcell/
It works great, but when I select an item in the ListView, the selected item value - bound to a ViewModel property - is null. Before I switched over to the custom cell, I used a default ViewCell, which works great. Selecting a row in the ListView raises an event and so I got the correct row object in the ViewModel.
After many hours of researching I finally found a possible error:
I used the CachingStrategy "RecycleElement" in the ListView, which causes the selected item to be null after forwarding it to the ViewModel. After setting the CachingStrategy to the default value "RetainElement", then everything works fine. This error only occurrs, if I use a custom cell.
I'm using the latest stable versions of Xamarin.iOS with Xamarin.Forms 184.108.40.206.
Can you attach a repro?
Created attachment 18718 [details]
Sample VS2012 project "ListViewCustomCellProj"
Set a breakpoint into the method
public void OnItemSelected(object sender, SelectedItemChangedEventArgs e)
in the code behind file MainPage.xaml.cs in the PCL. The propety "Item" in the SelectedItemChangedEventArgs will be null. Change the CachingStrategy of the ListView from "RecycleElement" to "RetainElement" and it works.
You could see the changes I made in that PR; however, I recommended an update on the tutorial that you were following since it doesn't seem to be targeted to RecycleElement. It could help us understand the issue.
Also, I'm not sure if going through custom renderers is a good idea. In your ListView ItemTemplate, why don't you create the labels in XAML there? https://developer.xamarin.com/guides/xamarin-forms/templates/data-templates/creating/
Overriding GetCell has present and future complications. For example, you're not doing the things XF is doing: https://github.com/xamarin/Xamarin.Forms/blob/master/Xamarin.Forms.Platform.iOS/Cells/ViewCellRenderer.cs#L11
I'd avoid custom renderers as much as possible.
thanks for your suggestions, I really appreciate that.
I thought, I follow the official Xamarin Forms recommendations:
"Consider a custom renderer if you need a very specific, complex design presented in your cells."
I'm using the custom cell and the platform specific renderer in only one specific case in my app, where a default ViewCell in the ItemTemplate works too slow for me, especially when I scroll through the ListView.
I agree that custom renderers should be used in specific, complex cases, but this requires you to have knowledge of individual platforms and the XF code you're bypassing. If you think ViewCell is too slow, maybe you can submit another bug with a minimal repro for us to figure out why it's slow.
Thanks a lot! :-)
I'll submit another bug with a repro in a few days.