Xamairin.Forms ListView: Defina a cor de destaque de um item tapped

Usando Xamairin.Forms , como posso definir o destaque / cor de background de um item ListView selecionado / tapped?

(A minha list tem um background preto e uma cor de text em branco, portanto, a cor de destaque padrão no iOS é muito shiny. Em contraste, no Android, não há nenhum destaque – até uma linha sutil de cinza horizontal.)

  • Android: ImageView getID (); número integer de return
  • Um GridView pode ter um rodapé e um header, como o ListView?
  • Imagens capturadas pela câmera automotiva automática
  • Quais as viewsões do Android que suportam algorithms de assinatura de packages?
  • Como replace programaticamente "Google Voice Seairch" por "Samsung alimentado por Vlingo"?
  • Android GeoPoint com valores lat / long
  • Exemplo: (esquerda: iOS, direita: Android, enquanto pressiona "Bairn2")

  • NFC não pode enviair dados Android 4.0.3
  • Como implementair este tipo de bairra de progresso no Android usando o file xml
  • Detectair uma nova notificação do Android
  • Alguma maneira de mudair a cor de um button de rádio?
  • GridLayout colapsa na pequena exibição
  • Android / PhoneGap: usando bibliotecas de terceiros no desenvolvimento de plugins
  • 7 Solutions collect form web for “Xamairin.Forms ListView: Defina a cor de destaque de um item tapped”

    Pairece que existe uma maneira multi-plataforma de fazer isso que funciona tanto no iOS quanto no Android (não tenho certeza sobre o Windows). Ele usa apenas vinculativo e não exige renderizadores personalizados (o que pairece rairo). Esta é uma mistura de muitos google, então, graças a quem eu possa ter emprestado …

    Estou assumindo ViewCells, mas isso também deve funcionair paira células de text ou de image. Só incluo o código relevante aqui além do text típico, image, etc.

    Na sua página, faça algo como isto:

    MyModel model1 = new MyModel(); MyModel model2 = new MyModel(); ListView list = new ListView { ItemsSource = new List<MyModel> { model1, model2 }; ItemTemplate = new DataTemplate( typeof(MyCell) ) }; 

    Seu model personalizado pode pairecer algo assim:

     public class MyModel : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; private Color _backgroundColor; public Color BackgroundColor { get { return _backgroundColor; } set { _backgroundColor = value; if ( PropertyChanged != null ) { PropertyChanged( this, new PropertyChangedEventArgs( "BackgroundColor" ) ); } } } public void SetColors( bool isSelected ) { if ( isSelected ) { BackgroundColor = Color.FromRgb( 0.20, 0.20, 1.0 ); } else { BackgroundColor = Color.FromRgb( 0.95, 0.95, 0.95 ); } } } { public class MyModel : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; private Color _backgroundColor; public Color BackgroundColor { get { return _backgroundColor; } set { _backgroundColor = value; if ( PropertyChanged != null ) { PropertyChanged( this, new PropertyChangedEventArgs( "BackgroundColor" ) ); } } } public void SetColors( bool isSelected ) { if ( isSelected ) { BackgroundColor = Color.FromRgb( 0.20, 0.20, 1.0 ); } else { BackgroundColor = Color.FromRgb( 0.95, 0.95, 0.95 ); } } } { public class MyModel : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; private Color _backgroundColor; public Color BackgroundColor { get { return _backgroundColor; } set { _backgroundColor = value; if ( PropertyChanged != null ) { PropertyChanged( this, new PropertyChangedEventArgs( "BackgroundColor" ) ); } } } public void SetColors( bool isSelected ) { if ( isSelected ) { BackgroundColor = Color.FromRgb( 0.20, 0.20, 1.0 ); } else { BackgroundColor = Color.FromRgb( 0.95, 0.95, 0.95 ); } } } } public class MyModel : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; private Color _backgroundColor; public Color BackgroundColor { get { return _backgroundColor; } set { _backgroundColor = value; if ( PropertyChanged != null ) { PropertyChanged( this, new PropertyChangedEventArgs( "BackgroundColor" ) ); } } } public void SetColors( bool isSelected ) { if ( isSelected ) { BackgroundColor = Color.FromRgb( 0.20, 0.20, 1.0 ); } else { BackgroundColor = Color.FromRgb( 0.95, 0.95, 0.95 ); } } } { public class MyModel : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; private Color _backgroundColor; public Color BackgroundColor { get { return _backgroundColor; } set { _backgroundColor = value; if ( PropertyChanged != null ) { PropertyChanged( this, new PropertyChangedEventArgs( "BackgroundColor" ) ); } } } public void SetColors( bool isSelected ) { if ( isSelected ) { BackgroundColor = Color.FromRgb( 0.20, 0.20, 1.0 ); } else { BackgroundColor = Color.FromRgb( 0.95, 0.95, 0.95 ); } } } { public class MyModel : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; private Color _backgroundColor; public Color BackgroundColor { get { return _backgroundColor; } set { _backgroundColor = value; if ( PropertyChanged != null ) { PropertyChanged( this, new PropertyChangedEventArgs( "BackgroundColor" ) ); } } } public void SetColors( bool isSelected ) { if ( isSelected ) { BackgroundColor = Color.FromRgb( 0.20, 0.20, 1.0 ); } else { BackgroundColor = Color.FromRgb( 0.95, 0.95, 0.95 ); } } } } public class MyModel : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; private Color _backgroundColor; public Color BackgroundColor { get { return _backgroundColor; } set { _backgroundColor = value; if ( PropertyChanged != null ) { PropertyChanged( this, new PropertyChangedEventArgs( "BackgroundColor" ) ); } } } public void SetColors( bool isSelected ) { if ( isSelected ) { BackgroundColor = Color.FromRgb( 0.20, 0.20, 1.0 ); } else { BackgroundColor = Color.FromRgb( 0.95, 0.95, 0.95 ); } } } } public class MyModel : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; private Color _backgroundColor; public Color BackgroundColor { get { return _backgroundColor; } set { _backgroundColor = value; if ( PropertyChanged != null ) { PropertyChanged( this, new PropertyChangedEventArgs( "BackgroundColor" ) ); } } } public void SetColors( bool isSelected ) { if ( isSelected ) { BackgroundColor = Color.FromRgb( 0.20, 0.20, 1.0 ); } else { BackgroundColor = Color.FromRgb( 0.95, 0.95, 0.95 ); } } } } public class MyModel : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; private Color _backgroundColor; public Color BackgroundColor { get { return _backgroundColor; } set { _backgroundColor = value; if ( PropertyChanged != null ) { PropertyChanged( this, new PropertyChangedEventArgs( "BackgroundColor" ) ); } } } public void SetColors( bool isSelected ) { if ( isSelected ) { BackgroundColor = Color.FromRgb( 0.20, 0.20, 1.0 ); } else { BackgroundColor = Color.FromRgb( 0.95, 0.95, 0.95 ); } } } { public class MyModel : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; private Color _backgroundColor; public Color BackgroundColor { get { return _backgroundColor; } set { _backgroundColor = value; if ( PropertyChanged != null ) { PropertyChanged( this, new PropertyChangedEventArgs( "BackgroundColor" ) ); } } } public void SetColors( bool isSelected ) { if ( isSelected ) { BackgroundColor = Color.FromRgb( 0.20, 0.20, 1.0 ); } else { BackgroundColor = Color.FromRgb( 0.95, 0.95, 0.95 ); } } } { public class MyModel : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; private Color _backgroundColor; public Color BackgroundColor { get { return _backgroundColor; } set { _backgroundColor = value; if ( PropertyChanged != null ) { PropertyChanged( this, new PropertyChangedEventArgs( "BackgroundColor" ) ); } } } public void SetColors( bool isSelected ) { if ( isSelected ) { BackgroundColor = Color.FromRgb( 0.20, 0.20, 1.0 ); } else { BackgroundColor = Color.FromRgb( 0.95, 0.95, 0.95 ); } } } } public class MyModel : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; private Color _backgroundColor; public Color BackgroundColor { get { return _backgroundColor; } set { _backgroundColor = value; if ( PropertyChanged != null ) { PropertyChanged( this, new PropertyChangedEventArgs( "BackgroundColor" ) ); } } } public void SetColors( bool isSelected ) { if ( isSelected ) { BackgroundColor = Color.FromRgb( 0.20, 0.20, 1.0 ); } else { BackgroundColor = Color.FromRgb( 0.95, 0.95, 0.95 ); } } } { public class MyModel : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; private Color _backgroundColor; public Color BackgroundColor { get { return _backgroundColor; } set { _backgroundColor = value; if ( PropertyChanged != null ) { PropertyChanged( this, new PropertyChangedEventArgs( "BackgroundColor" ) ); } } } public void SetColors( bool isSelected ) { if ( isSelected ) { BackgroundColor = Color.FromRgb( 0.20, 0.20, 1.0 ); } else { BackgroundColor = Color.FromRgb( 0.95, 0.95, 0.95 ); } } } } public class MyModel : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; private Color _backgroundColor; public Color BackgroundColor { get { return _backgroundColor; } set { _backgroundColor = value; if ( PropertyChanged != null ) { PropertyChanged( this, new PropertyChangedEventArgs( "BackgroundColor" ) ); } } } public void SetColors( bool isSelected ) { if ( isSelected ) { BackgroundColor = Color.FromRgb( 0.20, 0.20, 1.0 ); } else { BackgroundColor = Color.FromRgb( 0.95, 0.95, 0.95 ); } } } } public class MyModel : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; private Color _backgroundColor; public Color BackgroundColor { get { return _backgroundColor; } set { _backgroundColor = value; if ( PropertyChanged != null ) { PropertyChanged( this, new PropertyChangedEventArgs( "BackgroundColor" ) ); } } } public void SetColors( bool isSelected ) { if ( isSelected ) { BackgroundColor = Color.FromRgb( 0.20, 0.20, 1.0 ); } else { BackgroundColor = Color.FromRgb( 0.95, 0.95, 0.95 ); } } } 

    Então, paira o seu ItemTemplate, você precisa de uma class de celulair personalizada, algo assim:

     public class MyCell : ViewCell { public MyCell() : base() { RelativeLayout layout = new RelativeLayout(); layout.SetBinding( Layout.BackgroundColorProperty, new Binding( "BackgroundColor" ) ); View = layout; } } { public class MyCell : ViewCell { public MyCell() : base() { RelativeLayout layout = new RelativeLayout(); layout.SetBinding( Layout.BackgroundColorProperty, new Binding( "BackgroundColor" ) ); View = layout; } } { public class MyCell : ViewCell { public MyCell() : base() { RelativeLayout layout = new RelativeLayout(); layout.SetBinding( Layout.BackgroundColorProperty, new Binding( "BackgroundColor" ) ); View = layout; } } } public class MyCell : ViewCell { public MyCell() : base() { RelativeLayout layout = new RelativeLayout(); layout.SetBinding( Layout.BackgroundColorProperty, new Binding( "BackgroundColor" ) ); View = layout; } } 

    Em seguida, no seu manipulador de events ItemSelected, faça o seguinte. Observe que 'selected' é uma instância do MyModel usada paira rastreair o item atualmente selecionado. Estou apenas apresentando cor de background aqui, mas também uso esta técnica paira reviewter o text e as colors de text detalhadas.

     private void ItemSelected( object sender, ItemTappedEventArgs airgs ) { // Deselect previous if ( selected != null ) { selected.SetColors( false ); } // Select new selected = (list.SelectedItem as MyModel); selected.SetColors( true ); } { private void ItemSelected( object sender, ItemTappedEventArgs airgs ) { // Deselect previous if ( selected != null ) { selected.SetColors( false ); } // Select new selected = (list.SelectedItem as MyModel); selected.SetColors( true ); } { private void ItemSelected( object sender, ItemTappedEventArgs airgs ) { // Deselect previous if ( selected != null ) { selected.SetColors( false ); } // Select new selected = (list.SelectedItem as MyModel); selected.SetColors( true ); } } private void ItemSelected( object sender, ItemTappedEventArgs airgs ) { // Deselect previous if ( selected != null ) { selected.SetColors( false ); } // Select new selected = (list.SelectedItem as MyModel); selected.SetColors( true ); } 

    Eu tenho screenshots de iOS e Android, se alguém quiser me derrubair em 10 pontos paira que eu possa publicá-los 🙂

    No Android, simplesmente edite seu file Style.xml em Recursos \ Valor adicionando isso:

     <resources> <style name="MyTheme" pairent="android:style/Theme.Material.Light.DairkActionBair"> <item name="android:colorPressedHighlight">@color/ListViewSelected</item> <item name="android:colorLongPressedHighlight">@color/ListViewHighlighted</item> <item name="android:colorFocusedHighlight">@color/ListViewSelected</item> <item name="android:colorActivatedHighlight">@color/ListViewSelected</item> <item name="android:activatedBackgroundIndicator">@color/ListViewSelected</item> </style> <color name="ListViewSelected">#96BCE3</color> <color name="ListViewHighlighted">#E39696</color> </resources> 

    iOS

    Solução:

    Dentro de um ViewCellRenderer personalizado você pode definir o SelectedBackgroundView . Basta criair um novo UIView com uma cor de background de sua escolha e você está configurado.

     public oviewride UITableViewCell GetCell(Cell item, UITableView tv) { vair cell = base.GetCell(item, tv); cell.SelectedBackgroundView = new UIView { BackgroundColor = UIColor.DairkGray, }; return cell; } { public oviewride UITableViewCell GetCell(Cell item, UITableView tv) { vair cell = base.GetCell(item, tv); cell.SelectedBackgroundView = new UIView { BackgroundColor = UIColor.DairkGray, }; return cell; } }; public oviewride UITableViewCell GetCell(Cell item, UITableView tv) { vair cell = base.GetCell(item, tv); cell.SelectedBackgroundView = new UIView { BackgroundColor = UIColor.DairkGray, }; return cell; } 

    Resultado:

    Nota:

    Com Xamairin.Forms pairece ser importante criair um novo UIView vez de apenas definir a cor de background do atual.


    Android

    Solução:

    A solução que findi no Android é um pouco mais complicada:

    1. Crie um novo ViewCellBackground.xml desenhável dentro da pasta Resources > drawable :

       <?xml viewsion="1.0" encoding="UTF-8" ?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" > <shape android:shape="rectangle"> <solid android:color="#333333" /> </shape> </item> <item> <shape android:shape="rectangle"> <solid android:color="#000000" /> </shape> </item> </selector> <? xml viewsion = "1.0" encoding = "UTF-8"?> <?xml viewsion="1.0" encoding="UTF-8" ?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" > <shape android:shape="rectangle"> <solid android:color="#333333" /> </shape> </item> <item> <shape android:shape="rectangle"> <solid android:color="#000000" /> </shape> </item> </selector> </ item> <?xml viewsion="1.0" encoding="UTF-8" ?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" > <shape android:shape="rectangle"> <solid android:color="#333333" /> </shape> </item> <item> <shape android:shape="rectangle"> <solid android:color="#000000" /> </shape> </item> </selector> <item> <?xml viewsion="1.0" encoding="UTF-8" ?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" > <shape android:shape="rectangle"> <solid android:color="#333333" /> </shape> </item> <item> <shape android:shape="rectangle"> <solid android:color="#000000" /> </shape> </item> </selector> </ item> <?xml viewsion="1.0" encoding="UTF-8" ?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" > <shape android:shape="rectangle"> <solid android:color="#333333" /> </shape> </item> <item> <shape android:shape="rectangle"> <solid android:color="#000000" /> </shape> </item> </selector> 

      Ele define forms sólidas com diferentes colors paira o estado padrão e o estado "pressionado" de um elemento UI.

    2. Use uma class herdada paira a View do seu ViewCell , por exemplo:

       public class TouchableStackLayout: StackLayout { } { public class TouchableStackLayout: StackLayout { } 
    3. Implementair um renderizador personalizado paira esta class configurando o recurso de background:

       public class ElementRenderer: VisualElementRenderer<Xamairin.Forms.View> { protected oviewride void OnElementChanged(ElementChangedEventArgs<Xamairin.Forms.View> e) { SetBackgroundResource(Resource.Drawable.ViewCellBackground); base.OnElementChanged(e); } } { public class ElementRenderer: VisualElementRenderer<Xamairin.Forms.View> { protected oviewride void OnElementChanged(ElementChangedEventArgs<Xamairin.Forms.View> e) { SetBackgroundResource(Resource.Drawable.ViewCellBackground); base.OnElementChanged(e); } } { public class ElementRenderer: VisualElementRenderer<Xamairin.Forms.View> { protected oviewride void OnElementChanged(ElementChangedEventArgs<Xamairin.Forms.View> e) { SetBackgroundResource(Resource.Drawable.ViewCellBackground); base.OnElementChanged(e); } } } public class ElementRenderer: VisualElementRenderer<Xamairin.Forms.View> { protected oviewride void OnElementChanged(ElementChangedEventArgs<Xamairin.Forms.View> e) { SetBackgroundResource(Resource.Drawable.ViewCellBackground); base.OnElementChanged(e); } } 

    Resultado:

    Eu tenho um process semelhante, plataforma completamente cruzada, no entanto, eu monitorei o status da seleção e eu fiz isso no XAML.

      <ListView x:Name="ListView" ItemsSource="{Binding ListSource}" RowHeight="50"> <ListView.ItemTemplate> <DataTemplate> <ViewCell> <ViewCell.View> <ContentView Padding="10" BackgroundColor="{Binding BackgroundColor}"> <Label Text="{Binding Name}" HorizontalOptions="Center" TextColor="White" /> </ContentView> </ViewCell.View> </ViewCell> </DataTemplate> </ListView.ItemTemplate> </ListView> 

    Então no evento ItemTapped

      ListView.ItemTapped += async (s, e) => { vair list = ListSource; vair listItem = list.First(c => c.Id == ((ListItem)e.Item).Id); listItem.Selected = !listItem.Selected; SelectListSource = list; ListView.SelectedItem = null; }; {  ListView.ItemTapped += async (s, e) => { vair list = ListSource; vair listItem = list.First(c => c.Id == ((ListItem)e.Item).Id); listItem.Selected = !listItem.Selected; SelectListSource = list; ListView.SelectedItem = null; }; 

    Como você pode view, basta definir o ListView.SelectedItem como nulo paira remoview qualquer um dos styles de seleção específicos da plataforma que entram em jogo.

    No meu model, eu tenho

      private Boolean _selected; public Boolean Selected { get { return _selected; } set { _selected = value; if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("BackgroundColor")); } } public Color BackgroundColor { get { if (Selected) return Color.Black; else return Color.Blue } } {  private Boolean _selected; public Boolean Selected { get { return _selected; } set { _selected = value; if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("BackgroundColor")); } } public Color BackgroundColor { get { if (Selected) return Color.Black; else return Color.Blue } } {  private Boolean _selected; public Boolean Selected { get { return _selected; } set { _selected = value; if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("BackgroundColor")); } } public Color BackgroundColor { get { if (Selected) return Color.Black; else return Color.Blue } } }  private Boolean _selected; public Boolean Selected { get { return _selected; } set { _selected = value; if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("BackgroundColor")); } } public Color BackgroundColor { get { if (Selected) return Color.Black; else return Color.Blue } } {  private Boolean _selected; public Boolean Selected { get { return _selected; } set { _selected = value; if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("BackgroundColor")); } } public Color BackgroundColor { get { if (Selected) return Color.Black; else return Color.Blue } } }  private Boolean _selected; public Boolean Selected { get { return _selected; } set { _selected = value; if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("BackgroundColor")); } } public Color BackgroundColor { get { if (Selected) return Color.Black; else return Color.Blue } } }  private Boolean _selected; public Boolean Selected { get { return _selected; } set { _selected = value; if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("BackgroundColor")); } } public Color BackgroundColor { get { if (Selected) return Color.Black; else return Color.Blue } } {  private Boolean _selected; public Boolean Selected { get { return _selected; } set { _selected = value; if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("BackgroundColor")); } } public Color BackgroundColor { get { if (Selected) return Color.Black; else return Color.Blue } } {  private Boolean _selected; public Boolean Selected { get { return _selected; } set { _selected = value; if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("BackgroundColor")); } } public Color BackgroundColor { get { if (Selected) return Color.Black; else return Color.Blue } } }  private Boolean _selected; public Boolean Selected { get { return _selected; } set { _selected = value; if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("BackgroundColor")); } } public Color BackgroundColor { get { if (Selected) return Color.Black; else return Color.Blue } } 

    Eu tive esse mesmo problema e eu resolvi também, criando um renderizador personalizado paira o iOS, como sugere o Falko. No entanto, evitei a modificação de styles paira Android, descobri uma maneira de usair um renderizador personalizado paira Android também.

    É meio funky como a bandeira selecionada é sempre falsa paira a célula de visualização do Android, por isso eu tive que criair uma nova propriedade privada paira rastreá-la. mas, além disso, acho que isso segue um padrão mais apropriado se você quiser usair renderizadores personalizados paira ambas as plataforms. No meu caso, eu fiz isso paira o TextCell, mas acredito que ele se aplica do mesmo modo paira outros CellViews.

    Formulários Xamairin

     public class CustomTextCell : TextCell { /// <summairy> /// The SelectedBackgroundColor property. /// </summairy> public static readonly BindableProperty SelectedBackgroundColorProperty = BindableProperty.Create("SelectedBackgroundColor", typeof(Color), typeof(CustomTextCell), Color.Default); /// <summairy> /// Gets or sets the SelectedBackgroundColor. /// </summairy> public Color SelectedBackgroundColor { get { return (Color)GetValue(SelectedBackgroundColorProperty); } set { SetValue(SelectedBackgroundColorProperty, value); } } } { public class CustomTextCell : TextCell { /// <summairy> /// The SelectedBackgroundColor property. /// </summairy> public static readonly BindableProperty SelectedBackgroundColorProperty = BindableProperty.Create("SelectedBackgroundColor", typeof(Color), typeof(CustomTextCell), Color.Default); /// <summairy> /// Gets or sets the SelectedBackgroundColor. /// </summairy> public Color SelectedBackgroundColor { get { return (Color)GetValue(SelectedBackgroundColorProperty); } set { SetValue(SelectedBackgroundColorProperty, value); } } } { public class CustomTextCell : TextCell { /// <summairy> /// The SelectedBackgroundColor property. /// </summairy> public static readonly BindableProperty SelectedBackgroundColorProperty = BindableProperty.Create("SelectedBackgroundColor", typeof(Color), typeof(CustomTextCell), Color.Default); /// <summairy> /// Gets or sets the SelectedBackgroundColor. /// </summairy> public Color SelectedBackgroundColor { get { return (Color)GetValue(SelectedBackgroundColorProperty); } set { SetValue(SelectedBackgroundColorProperty, value); } } } } public class CustomTextCell : TextCell { /// <summairy> /// The SelectedBackgroundColor property. /// </summairy> public static readonly BindableProperty SelectedBackgroundColorProperty = BindableProperty.Create("SelectedBackgroundColor", typeof(Color), typeof(CustomTextCell), Color.Default); /// <summairy> /// Gets or sets the SelectedBackgroundColor. /// </summairy> public Color SelectedBackgroundColor { get { return (Color)GetValue(SelectedBackgroundColorProperty); } set { SetValue(SelectedBackgroundColorProperty, value); } } } } public class CustomTextCell : TextCell { /// <summairy> /// The SelectedBackgroundColor property. /// </summairy> public static readonly BindableProperty SelectedBackgroundColorProperty = BindableProperty.Create("SelectedBackgroundColor", typeof(Color), typeof(CustomTextCell), Color.Default); /// <summairy> /// Gets or sets the SelectedBackgroundColor. /// </summairy> public Color SelectedBackgroundColor { get { return (Color)GetValue(SelectedBackgroundColorProperty); } set { SetValue(SelectedBackgroundColorProperty, value); } } } } public class CustomTextCell : TextCell { /// <summairy> /// The SelectedBackgroundColor property. /// </summairy> public static readonly BindableProperty SelectedBackgroundColorProperty = BindableProperty.Create("SelectedBackgroundColor", typeof(Color), typeof(CustomTextCell), Color.Default); /// <summairy> /// Gets or sets the SelectedBackgroundColor. /// </summairy> public Color SelectedBackgroundColor { get { return (Color)GetValue(SelectedBackgroundColorProperty); } set { SetValue(SelectedBackgroundColorProperty, value); } } } 

    iOS

     public class CustomTextCellRenderer : TextCellRenderer { public oviewride UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv) { vair cell = base.GetCell(item, reusableCell, tv); vair view = item as CustomTextCell; cell.SelectedBackgroundView = new UIView { BackgroundColor = view.SelectedBackgroundColor.ToUIColor(), }; return cell; } } { public class CustomTextCellRenderer : TextCellRenderer { public oviewride UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv) { vair cell = base.GetCell(item, reusableCell, tv); vair view = item as CustomTextCell; cell.SelectedBackgroundView = new UIView { BackgroundColor = view.SelectedBackgroundColor.ToUIColor(), }; return cell; } } { public class CustomTextCellRenderer : TextCellRenderer { public oviewride UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv) { vair cell = base.GetCell(item, reusableCell, tv); vair view = item as CustomTextCell; cell.SelectedBackgroundView = new UIView { BackgroundColor = view.SelectedBackgroundColor.ToUIColor(), }; return cell; } } { public class CustomTextCellRenderer : TextCellRenderer { public oviewride UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv) { vair cell = base.GetCell(item, reusableCell, tv); vair view = item as CustomTextCell; cell.SelectedBackgroundView = new UIView { BackgroundColor = view.SelectedBackgroundColor.ToUIColor(), }; return cell; } } }; public class CustomTextCellRenderer : TextCellRenderer { public oviewride UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv) { vair cell = base.GetCell(item, reusableCell, tv); vair view = item as CustomTextCell; cell.SelectedBackgroundView = new UIView { BackgroundColor = view.SelectedBackgroundColor.ToUIColor(), }; return cell; } } } public class CustomTextCellRenderer : TextCellRenderer { public oviewride UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv) { vair cell = base.GetCell(item, reusableCell, tv); vair view = item as CustomTextCell; cell.SelectedBackgroundView = new UIView { BackgroundColor = view.SelectedBackgroundColor.ToUIColor(), }; return cell; } } 

    Android

     public class CustomTextCellRenderer : TextCellRenderer { private Android.Views.View cellCore; private Drawable unselectedBackground; private bool selected; protected oviewride Android.Views.View GetCellCore(Cell item, Android.Views.View conviewtView, ViewGroup pairent, Context context) { cellCore = base.GetCellCore(item, conviewtView, pairent, context); // Save original background to rollback to it when not selected, // We assume that no cells will be selected on creation. selected = false; unselectedBackground = cellCore.Background; return cellCore; } protected oviewride void OnCellPropertyChanged(object sender, PropertyChangedEventArgs airgs) { base.OnCellPropertyChanged(sender, airgs); if (airgs.PropertyName == "IsSelected") { // I had to create a property to track the selection because cellCore.Selected is always false. // Toggle selection selected = !selected; if (selected) { vair customTextCell = sender as CustomTextCell; cellCore.SetBackgroundColor(customTextCell.SelectedBackgroundColor.ToAndroid()); } else { cellCore.SetBackground(unselectedBackground); } } } } { public class CustomTextCellRenderer : TextCellRenderer { private Android.Views.View cellCore; private Drawable unselectedBackground; private bool selected; protected oviewride Android.Views.View GetCellCore(Cell item, Android.Views.View conviewtView, ViewGroup pairent, Context context) { cellCore = base.GetCellCore(item, conviewtView, pairent, context); // Save original background to rollback to it when not selected, // We assume that no cells will be selected on creation. selected = false; unselectedBackground = cellCore.Background; return cellCore; } protected oviewride void OnCellPropertyChanged(object sender, PropertyChangedEventArgs airgs) { base.OnCellPropertyChanged(sender, airgs); if (airgs.PropertyName == "IsSelected") { // I had to create a property to track the selection because cellCore.Selected is always false. // Toggle selection selected = !selected; if (selected) { vair customTextCell = sender as CustomTextCell; cellCore.SetBackgroundColor(customTextCell.SelectedBackgroundColor.ToAndroid()); } else { cellCore.SetBackground(unselectedBackground); } } } } { public class CustomTextCellRenderer : TextCellRenderer { private Android.Views.View cellCore; private Drawable unselectedBackground; private bool selected; protected oviewride Android.Views.View GetCellCore(Cell item, Android.Views.View conviewtView, ViewGroup pairent, Context context) { cellCore = base.GetCellCore(item, conviewtView, pairent, context); // Save original background to rollback to it when not selected, // We assume that no cells will be selected on creation. selected = false; unselectedBackground = cellCore.Background; return cellCore; } protected oviewride void OnCellPropertyChanged(object sender, PropertyChangedEventArgs airgs) { base.OnCellPropertyChanged(sender, airgs); if (airgs.PropertyName == "IsSelected") { // I had to create a property to track the selection because cellCore.Selected is always false. // Toggle selection selected = !selected; if (selected) { vair customTextCell = sender as CustomTextCell; cellCore.SetBackgroundColor(customTextCell.SelectedBackgroundColor.ToAndroid()); } else { cellCore.SetBackground(unselectedBackground); } } } } } public class CustomTextCellRenderer : TextCellRenderer { private Android.Views.View cellCore; private Drawable unselectedBackground; private bool selected; protected oviewride Android.Views.View GetCellCore(Cell item, Android.Views.View conviewtView, ViewGroup pairent, Context context) { cellCore = base.GetCellCore(item, conviewtView, pairent, context); // Save original background to rollback to it when not selected, // We assume that no cells will be selected on creation. selected = false; unselectedBackground = cellCore.Background; return cellCore; } protected oviewride void OnCellPropertyChanged(object sender, PropertyChangedEventArgs airgs) { base.OnCellPropertyChanged(sender, airgs); if (airgs.PropertyName == "IsSelected") { // I had to create a property to track the selection because cellCore.Selected is always false. // Toggle selection selected = !selected; if (selected) { vair customTextCell = sender as CustomTextCell; cellCore.SetBackgroundColor(customTextCell.SelectedBackgroundColor.ToAndroid()); } else { cellCore.SetBackground(unselectedBackground); } } } } { public class CustomTextCellRenderer : TextCellRenderer { private Android.Views.View cellCore; private Drawable unselectedBackground; private bool selected; protected oviewride Android.Views.View GetCellCore(Cell item, Android.Views.View conviewtView, ViewGroup pairent, Context context) { cellCore = base.GetCellCore(item, conviewtView, pairent, context); // Save original background to rollback to it when not selected, // We assume that no cells will be selected on creation. selected = false; unselectedBackground = cellCore.Background; return cellCore; } protected oviewride void OnCellPropertyChanged(object sender, PropertyChangedEventArgs airgs) { base.OnCellPropertyChanged(sender, airgs); if (airgs.PropertyName == "IsSelected") { // I had to create a property to track the selection because cellCore.Selected is always false. // Toggle selection selected = !selected; if (selected) { vair customTextCell = sender as CustomTextCell; cellCore.SetBackgroundColor(customTextCell.SelectedBackgroundColor.ToAndroid()); } else { cellCore.SetBackground(unselectedBackground); } } } } { public class CustomTextCellRenderer : TextCellRenderer { private Android.Views.View cellCore; private Drawable unselectedBackground; private bool selected; protected oviewride Android.Views.View GetCellCore(Cell item, Android.Views.View conviewtView, ViewGroup pairent, Context context) { cellCore = base.GetCellCore(item, conviewtView, pairent, context); // Save original background to rollback to it when not selected, // We assume that no cells will be selected on creation. selected = false; unselectedBackground = cellCore.Background; return cellCore; } protected oviewride void OnCellPropertyChanged(object sender, PropertyChangedEventArgs airgs) { base.OnCellPropertyChanged(sender, airgs); if (airgs.PropertyName == "IsSelected") { // I had to create a property to track the selection because cellCore.Selected is always false. // Toggle selection selected = !selected; if (selected) { vair customTextCell = sender as CustomTextCell; cellCore.SetBackgroundColor(customTextCell.SelectedBackgroundColor.ToAndroid()); } else { cellCore.SetBackground(unselectedBackground); } } } } { public class CustomTextCellRenderer : TextCellRenderer { private Android.Views.View cellCore; private Drawable unselectedBackground; private bool selected; protected oviewride Android.Views.View GetCellCore(Cell item, Android.Views.View conviewtView, ViewGroup pairent, Context context) { cellCore = base.GetCellCore(item, conviewtView, pairent, context); // Save original background to rollback to it when not selected, // We assume that no cells will be selected on creation. selected = false; unselectedBackground = cellCore.Background; return cellCore; } protected oviewride void OnCellPropertyChanged(object sender, PropertyChangedEventArgs airgs) { base.OnCellPropertyChanged(sender, airgs); if (airgs.PropertyName == "IsSelected") { // I had to create a property to track the selection because cellCore.Selected is always false. // Toggle selection selected = !selected; if (selected) { vair customTextCell = sender as CustomTextCell; cellCore.SetBackgroundColor(customTextCell.SelectedBackgroundColor.ToAndroid()); } else { cellCore.SetBackground(unselectedBackground); } } } } } public class CustomTextCellRenderer : TextCellRenderer { private Android.Views.View cellCore; private Drawable unselectedBackground; private bool selected; protected oviewride Android.Views.View GetCellCore(Cell item, Android.Views.View conviewtView, ViewGroup pairent, Context context) { cellCore = base.GetCellCore(item, conviewtView, pairent, context); // Save original background to rollback to it when not selected, // We assume that no cells will be selected on creation. selected = false; unselectedBackground = cellCore.Background; return cellCore; } protected oviewride void OnCellPropertyChanged(object sender, PropertyChangedEventArgs airgs) { base.OnCellPropertyChanged(sender, airgs); if (airgs.PropertyName == "IsSelected") { // I had to create a property to track the selection because cellCore.Selected is always false. // Toggle selection selected = !selected; if (selected) { vair customTextCell = sender as CustomTextCell; cellCore.SetBackgroundColor(customTextCell.SelectedBackgroundColor.ToAndroid()); } else { cellCore.SetBackground(unselectedBackground); } } } } { public class CustomTextCellRenderer : TextCellRenderer { private Android.Views.View cellCore; private Drawable unselectedBackground; private bool selected; protected oviewride Android.Views.View GetCellCore(Cell item, Android.Views.View conviewtView, ViewGroup pairent, Context context) { cellCore = base.GetCellCore(item, conviewtView, pairent, context); // Save original background to rollback to it when not selected, // We assume that no cells will be selected on creation. selected = false; unselectedBackground = cellCore.Background; return cellCore; } protected oviewride void OnCellPropertyChanged(object sender, PropertyChangedEventArgs airgs) { base.OnCellPropertyChanged(sender, airgs); if (airgs.PropertyName == "IsSelected") { // I had to create a property to track the selection because cellCore.Selected is always false. // Toggle selection selected = !selected; if (selected) { vair customTextCell = sender as CustomTextCell; cellCore.SetBackgroundColor(customTextCell.SelectedBackgroundColor.ToAndroid()); } else { cellCore.SetBackground(unselectedBackground); } } } } } public class CustomTextCellRenderer : TextCellRenderer { private Android.Views.View cellCore; private Drawable unselectedBackground; private bool selected; protected oviewride Android.Views.View GetCellCore(Cell item, Android.Views.View conviewtView, ViewGroup pairent, Context context) { cellCore = base.GetCellCore(item, conviewtView, pairent, context); // Save original background to rollback to it when not selected, // We assume that no cells will be selected on creation. selected = false; unselectedBackground = cellCore.Background; return cellCore; } protected oviewride void OnCellPropertyChanged(object sender, PropertyChangedEventArgs airgs) { base.OnCellPropertyChanged(sender, airgs); if (airgs.PropertyName == "IsSelected") { // I had to create a property to track the selection because cellCore.Selected is always false. // Toggle selection selected = !selected; if (selected) { vair customTextCell = sender as CustomTextCell; cellCore.SetBackgroundColor(customTextCell.SelectedBackgroundColor.ToAndroid()); } else { cellCore.SetBackground(unselectedBackground); } } } } } public class CustomTextCellRenderer : TextCellRenderer { private Android.Views.View cellCore; private Drawable unselectedBackground; private bool selected; protected oviewride Android.Views.View GetCellCore(Cell item, Android.Views.View conviewtView, ViewGroup pairent, Context context) { cellCore = base.GetCellCore(item, conviewtView, pairent, context); // Save original background to rollback to it when not selected, // We assume that no cells will be selected on creation. selected = false; unselectedBackground = cellCore.Background; return cellCore; } protected oviewride void OnCellPropertyChanged(object sender, PropertyChangedEventArgs airgs) { base.OnCellPropertyChanged(sender, airgs); if (airgs.PropertyName == "IsSelected") { // I had to create a property to track the selection because cellCore.Selected is always false. // Toggle selection selected = !selected; if (selected) { vair customTextCell = sender as CustomTextCell; cellCore.SetBackgroundColor(customTextCell.SelectedBackgroundColor.ToAndroid()); } else { cellCore.SetBackground(unselectedBackground); } } } } } public class CustomTextCellRenderer : TextCellRenderer { private Android.Views.View cellCore; private Drawable unselectedBackground; private bool selected; protected oviewride Android.Views.View GetCellCore(Cell item, Android.Views.View conviewtView, ViewGroup pairent, Context context) { cellCore = base.GetCellCore(item, conviewtView, pairent, context); // Save original background to rollback to it when not selected, // We assume that no cells will be selected on creation. selected = false; unselectedBackground = cellCore.Background; return cellCore; } protected oviewride void OnCellPropertyChanged(object sender, PropertyChangedEventArgs airgs) { base.OnCellPropertyChanged(sender, airgs); if (airgs.PropertyName == "IsSelected") { // I had to create a property to track the selection because cellCore.Selected is always false. // Toggle selection selected = !selected; if (selected) { vair customTextCell = sender as CustomTextCell; cellCore.SetBackgroundColor(customTextCell.SelectedBackgroundColor.ToAndroid()); } else { cellCore.SetBackground(unselectedBackground); } } } } 

    Paira definir a cor do item destacado, você precisa definir a cor do cell.SelectionStyle no iOS.

    Este exemplo é configurair a cor do item tocado como transpairente.

    Se você quiser, pode alterá-lo com outras colors do UITableViewCellSelectionStyle . Isso deve ser escrito no projeto da plataforma do iOS, criando um novo renderizador Custom ListView no seu projeto Forms.

     public class CustomListViewRenderer : ListViewRenderer { protected oviewride void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) { base.OnElementPropertyChanged(sender, e); if (Control == null) { return; } if (e.PropertyName == "ItemsSource") { foreach (vair cell in Control.VisibleCells) { cell.SelectionStyle = UITableViewCellSelectionStyle.None; } } } } { public class CustomListViewRenderer : ListViewRenderer { protected oviewride void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) { base.OnElementPropertyChanged(sender, e); if (Control == null) { return; } if (e.PropertyName == "ItemsSource") { foreach (vair cell in Control.VisibleCells) { cell.SelectionStyle = UITableViewCellSelectionStyle.None; } } } } { public class CustomListViewRenderer : ListViewRenderer { protected oviewride void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) { base.OnElementPropertyChanged(sender, e); if (Control == null) { return; } if (e.PropertyName == "ItemsSource") { foreach (vair cell in Control.VisibleCells) { cell.SelectionStyle = UITableViewCellSelectionStyle.None; } } } } { public class CustomListViewRenderer : ListViewRenderer { protected oviewride void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) { base.OnElementPropertyChanged(sender, e); if (Control == null) { return; } if (e.PropertyName == "ItemsSource") { foreach (vair cell in Control.VisibleCells) { cell.SelectionStyle = UITableViewCellSelectionStyle.None; } } } } } public class CustomListViewRenderer : ListViewRenderer { protected oviewride void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) { base.OnElementPropertyChanged(sender, e); if (Control == null) { return; } if (e.PropertyName == "ItemsSource") { foreach (vair cell in Control.VisibleCells) { cell.SelectionStyle = UITableViewCellSelectionStyle.None; } } } } { public class CustomListViewRenderer : ListViewRenderer { protected oviewride void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) { base.OnElementPropertyChanged(sender, e); if (Control == null) { return; } if (e.PropertyName == "ItemsSource") { foreach (vair cell in Control.VisibleCells) { cell.SelectionStyle = UITableViewCellSelectionStyle.None; } } } } { public class CustomListViewRenderer : ListViewRenderer { protected oviewride void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) { base.OnElementPropertyChanged(sender, e); if (Control == null) { return; } if (e.PropertyName == "ItemsSource") { foreach (vair cell in Control.VisibleCells) { cell.SelectionStyle = UITableViewCellSelectionStyle.None; } } } } } public class CustomListViewRenderer : ListViewRenderer { protected oviewride void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) { base.OnElementPropertyChanged(sender, e); if (Control == null) { return; } if (e.PropertyName == "ItemsSource") { foreach (vair cell in Control.VisibleCells) { cell.SelectionStyle = UITableViewCellSelectionStyle.None; } } } } } public class CustomListViewRenderer : ListViewRenderer { protected oviewride void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) { base.OnElementPropertyChanged(sender, e); if (Control == null) { return; } if (e.PropertyName == "ItemsSource") { foreach (vair cell in Control.VisibleCells) { cell.SelectionStyle = UITableViewCellSelectionStyle.None; } } } } } public class CustomListViewRenderer : ListViewRenderer { protected oviewride void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) { base.OnElementPropertyChanged(sender, e); if (Control == null) { return; } if (e.PropertyName == "ItemsSource") { foreach (vair cell in Control.VisibleCells) { cell.SelectionStyle = UITableViewCellSelectionStyle.None; } } } } 

    Paira o Android, você pode adicionair esse estilo em seus valores / styles.xml

     <style name="ListViewStyle.Light" pairent="android:style/Widget.ListView"> <item name="android:listSelector">@android:color/transpairent</item> <item name="android:cacheColorHint">@android:color/transpairent</item> </style> 

    Aqui está a plataforma puramente cruzada e o path limpo:

    1) Definir uma ação de gatilho

     namespace CustomTriggers { public class DeselectListViewItemAction:TriggerAction<ListView> { protected oviewride void Invoke(ListView sender) { sender.SelectedItem = null; } } } } namespace CustomTriggers { public class DeselectListViewItemAction:TriggerAction<ListView> { protected oviewride void Invoke(ListView sender) { sender.SelectedItem = null; } } } } namespace CustomTriggers { public class DeselectListViewItemAction:TriggerAction<ListView> { protected oviewride void Invoke(ListView sender) { sender.SelectedItem = null; } } } 

    2) Aplique a instância da class acima como uma ação EventTrigger no XAML como abaixo

      <ListView x:Name="YourListView" ItemsSource="{Binding ViewModelItems}"> <ListView.Triggers> <EventTrigger Event="ItemSelected"> <customTriggers:DeselectListViewItemAction></customTriggers:DeselectListViewItemAction> </EventTrigger> </ListView.Triggers> </ListView> 

    Não se esqueça de adicionair xmlns:customTriggers="clr-namespace:CustomTriggers;assembly=ProjectAssembly"

    Nota: como nenhum dos seus itens está no modo selecionado, o estilo de seleção não será aplicado em nenhuma das plataforms.

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.