Вам нужно будет добавить настраиваемую ячейку просмотра, в которой вы можете установить цвет при нажатии:
В вашем PCL:
Добавить пользовательский ViewCell примерно так:
public class ExtendedViewCell : ViewCell
{
/// <summary>
/// The SelectedBackgroundColor property.
/// </summary>
public static readonly BindableProperty SelectedBackgroundColorProperty =
BindableProperty.Create("SelectedBackgroundColor", typeof(Color), typeof(ExtendedViewCell), Color.Default);
/// <summary>
/// Gets or sets the SelectedBackgroundColor.
/// </summary>
public Color SelectedBackgroundColor
{
get { return (Color)GetValue(SelectedBackgroundColorProperty); }
set { SetValue(SelectedBackgroundColorProperty, value); }
}
}
В вашем Android Project добавьте следующее:
public class ExtendedViewCellRenderer : ViewCellRenderer
{
private Android.Views.View _cellCore;
private Drawable _unselectedBackground;
private bool _selected;
protected override Android.Views.View GetCellCore(Cell item, Android.Views.View convertView, ViewGroup parent, Context context)
{
try
{
_cellCore = base.GetCellCore(item, convertView, parent, context);
// Save original background to roll-back to it when not selected,
// we're assuming that no cells will be selected on creation.
_selected = false;
_unselectedBackground = _cellCore.Background;
return _cellCore;
}
catch(Exception ex)
{
AppLogger.LogException(ex);
return null;
}
}
protected override void OnCellPropertyChanged(object sender, PropertyChangedEventArgs args)
{
try
{
base.OnCellPropertyChanged(sender, args);
if (args.PropertyName == "IsSelected")
{
// Had to create a property to track the selection because cellCore.Selected is always false.
_selected = !_selected;
if (_selected)
{
var extendedViewCell = sender as ExtendedViewCell;
_cellCore.SetBackgroundColor(extendedViewCell.SelectedBackgroundColor.ToAndroid());
}
else
{
_cellCore.SetBackground(_unselectedBackground);
}
}
}
catch(Exception ex)
{
AppLogger.LogException(ex);
}
}
}
Аналогично для iOS примерно так:
public class ExtendedViewCellRenderer : ViewCellRenderer
{
public override UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv)
{
try
{
var cell = base.GetCell(item, reusableCell, tv);
var view = item as ExtendedViewCell;
cell.SelectedBackgroundView = new UIView
{
BackgroundColor = view.SelectedBackgroundColor.ToUIColor(),
};
cell.SelectionStyle = UITableViewCellSelectionStyle.None;
return cell;
}
catch(Exception ex)
{
AppLogger.LogException(ex);
return null;
}
}
}
Теперь не забудьте добавитьнастраиваемый заголовок средства визуализации в собственных классах над пространством имен
[assembly: ExportRenderer(typeof(ExtendedViewCell), typeof(ExtendedViewCellRenderer))]
Теперь все, что вам нужно сделать, - заменить вышеприведенный ViewCell на эту ячейку представления элементов управления и передать SelectedBackgroundProperty.
В вашем случаеэто будет выглядеть так:
<ListView x:Name="Llist"
ItemTapped="Lista_ItemTapped" >
<ListView.ItemTemplate>
<DataTemplate>
<nameSpace:ExtendedViewCell SelectedBackgroundColor="White">
<StackLayout BackgroundColor="{Binding color}">
<Label Text="{Binding name}"/>
<Label Text="{Binding age}"/>
<Label Text="{Binding sex}"/>
</StackLayout>
</nameSpace:ExtendedViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>