After updating to the newest version of Xamarin.Forms none of ViewCells can be selected no more(on iOS. on Android everything is fine). To make sure, that this error wasn't in my code i've created a new blank solution and tested simple ListView and get same glitch. I only see 1px height blue line when item is selected.
<StackLayout> <Label Text="Welcome to Xamarin.Forms!" HorizontalOptions="Start" VerticalOptions="CenterAndExpand" /> <ListView ItemsSource="{Binding Items}" SelectionMode="Single" SeparatorColor="Red"> <ListView.ItemTemplate> <DataTemplate> <TextCell Text="{Binding .}"/> </DataTemplate> </ListView.ItemTemplate> </ListView> </StackLayout>
Okay. I've rolled back to Xamarin.Forms 3.5.0.274416 and Xamarin.Essentials 1.2.0 and selection works like a charm.
So here i am. To fix this problem i've created Custom Renderer. But for ListView, not ViewCell(but it also can be fixed with custom renderer for ViewCell ).
Here is the code: [assembly: ExportRenderer(typeof(ListView), typeof(StandardListViewRenderer))] namespace App.CrossPlatform.iOS.CustomRenderers { public class StandardListViewRenderer : ListViewRenderer { protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) { try { base.OnElementPropertyChanged(sender, e); if (Control == null) return; if (!((ListView)sender).IsRefreshing) { var selection = Control.IndexPathForSelectedRow; Control.ReloadData(); if (selection != null) { var row = Control.CellAt(selection); row.ContentView.BackgroundColor = new UIColor(0, 0, 0, 0f); row.SelectionStyle = UITableViewCellSelectionStyle.Blue; Control.SelectRow(selection, false, UITableViewScrollPosition.None); } } } catch { // ignored } } } }
As far as i understand ContentView's background colour is no more Transparent, so selection can't be visible. After setting it to transparent everything works like a charm.
This is code if you'd like to use Custom Renderer for ViewCell, not ListView:
public override UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv) { var cell = base.GetCell(item, reusableCell, tv); cell.ContentView.BackgroundColor = new UIColor(0, 0, 0, 0f); cell.SelectionStyle = UITableViewCellSelectionStyle.Blue; return cell; }
Answers
I had the same problem but didn't even realise it was a bug. I just thought I messed something up.
If you want to report it I think you can do that here https://github.com/xamarin/xamarin.forms/issues
However, for me I solved it by giving my custom item cell a Background and in its model a IsSelected state.
Bind the Background to a Color property and return your unselected and selected color in its get according to the IsSelected state.
I change
TextCell
toViewCell
and change Selection Style in custom renderer like below , but still no luck , it should be the issue .I suggest you raise issue here : https://github.com/xamarin/Xamarin.Forms/issues .
Okay. I've rolled back to Xamarin.Forms 3.5.0.274416 and Xamarin.Essentials 1.2.0 and selection works like a charm.
Please mark it as answer though it's not a nice solution ..
So here i am. To fix this problem i've created Custom Renderer. But for ListView, not ViewCell(but it also can be fixed with custom renderer for ViewCell ).
Here is the code:
[assembly: ExportRenderer(typeof(ListView), typeof(StandardListViewRenderer))] namespace App.CrossPlatform.iOS.CustomRenderers { public class StandardListViewRenderer : ListViewRenderer { protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) { try { base.OnElementPropertyChanged(sender, e); if (Control == null) return; if (!((ListView)sender).IsRefreshing) { var selection = Control.IndexPathForSelectedRow; Control.ReloadData(); if (selection != null) { var row = Control.CellAt(selection); row.ContentView.BackgroundColor = new UIColor(0, 0, 0, 0f); row.SelectionStyle = UITableViewCellSelectionStyle.Blue; Control.SelectRow(selection, false, UITableViewScrollPosition.None); } } } catch { // ignored } } } }
As far as i understand ContentView's background colour is no more Transparent, so selection can't be visible. After setting it to transparent everything works like a charm.
This is code if you'd like to use Custom Renderer for ViewCell, not ListView:
public override UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv) { var cell = base.GetCell(item, reusableCell, tv); cell.ContentView.BackgroundColor = new UIColor(0, 0, 0, 0f); cell.SelectionStyle = UITableViewCellSelectionStyle.Blue; return cell; }