列表视图xamarin.forms中selecteditem的背景色 [英] Background color of selecteditem in listview xamarin.forms

查看:84
本文介绍了列表视图xamarin.forms中selecteditem的背景色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Xamarin.forms中使用ListView时,在选择一个项目时,该项目仍然具有类似的丑陋背景色.我可以禁用此功能吗?

When working with ListView in Xamarin.forms, on selecting an item, the item remains like that with a rather ugly background colour. Can i disable this feature?

这是我的代码:

<StackLayout>
  <ListView x:Name="FoodList" HasUnevenRows="True" CachingStrategy="RecycleElement" ItemTapped="OnItemTapped">
    <ListView.ItemTemplate>
      <DataTemplate>
        <ViewCell>
          <StackLayout Padding="0,15,0,0" >
           <StackLayout Orientation="Horizontal" BackgroundColor="White" >
            <Image Source="{Binding image_url}" Aspect="AspectFill" HeightRequest="200" WidthRequest="200"/>
            <StackLayout Orientation="Vertical">
              <Label Text="{Binding food_name}" TextColor="Black" Style="{StaticResource MainLisTtext}" />
              <Label Text="{Binding price}" TextColor="Black" Style="{StaticResource SubLisTtext}" />
              <Label Text="{Binding food_description}" TextColor="Black" Style="{StaticResource SubLisTtext}" />
            </StackLayout>
            </StackLayout>
          </StackLayout>
        </ViewCell>vc
      </DataTemplate>
    </ListView.ItemTemplate>
  </ListView>
</StackLayout>

推荐答案

这可以通过使用自定义渲染器"来实现

This can be accomplished by using Custom Renderers

在iOS上编辑SelectionStyle属性.

下面是将UITableViewCellSelectionStyle设置为None的示例.

Below is an example that sets the UITableViewCellSelectionStyle to None.

using System;

using UIKit;

using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;

using ListViewSample.iOS;

[assembly: ExportRenderer(typeof(ViewCell), typeof(ViewCellItemSelectedCustomRenderer))]
namespace ListViewSample.iOS
{
    public class ViewCellItemSelectedCustomRenderer : ViewCellRenderer
    {
        public override UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv)
        {
            var cell = base.GetCell(item, reusableCell, tv);

            cell.SelectionStyle = UITableViewCellSelectionStyle.None;

            return cell;
        }
    }
}

Android自定义渲染器

  1. 创建一个新的可绘制对象ViewCellBackground.xml并将其保存到> drawable文件夹中:

  1. Create a new drawable, ViewCellBackground.xml and save it to the Resources>drawable folder:

<?xml version="1.0" encoding="UTF-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" >
        <shape android:shape="rectangle">
            <!--Change the selected color by modifying this hex value-->
            <solid android:color="#FFFFFF" />
        </shape>
    </item>
    <item>
        <shape android:shape="rectangle">
            <solid android:color="#FFFFFF" />
        </shape>
    </item>
</selector>

  • 为ViewCell创建自定义渲染器

  • Create Custom Renderer for the ViewCell

    using System;
    
    using Xamarin.Forms;
    using Xamarin.Forms.Platform.Android;
    
    using ListViewSample.Droid;
    
    [assembly: ExportRenderer(typeof(ViewCell), typeof(ViewCellItemSelectedCustomRenderer))]
    namespace ListViewSample.Droid
    {
        public class ViewCellItemSelectedCustomRenderer : ViewCellRenderer
        {
            protected override Android.Views.View GetCellCore(Cell item, Android.Views.View convertView, Android.Views.ViewGroup parent, Android.Content.Context context)
            {
                var cell = base.GetCellCore(item, convertView, parent, context);
    
                cell.SetBackgroundResource(Resource.Drawable.ViewCellBackground);
    
                return cell;
            }
        }
    }
    

  • 删除了没有自定义渲染器的实现

    Removed implementation without Custom Renderers

    using System;
    using System.Collections.Generic;
    
    using Xamarin.Forms;
    
    namespace ListViewSample
    {
        public class CustomViewCell : ViewCell
        {
            public CustomViewCell()
            {
                View = new Label
                {
                    Text = "Hello World"
                };
            }
        }
    
        public class ListViewContentPage : ContentPage
        {
            public ListViewContentPage()
            {
                var itemSourceList = new List<CustomViewCell>();
                itemSourceList.Add(new CustomViewCell());
                itemSourceList.Add(new CustomViewCell());
    
                var listView = new ListView();
                listView.ItemTemplate = new DataTemplate(typeof(CustomViewCell));
                listView.ItemsSource = itemSourceList;
                listView.SeparatorVisibility = SeparatorVisibility.None;
    
                Content = listView;
            }
        }
    
        public class App : Application
        {
            public App()
            {
                // The root page of your application
    
                MainPage = new NavigationPage(new ListViewContentPage());
            }
        }
    }
    

    这篇关于列表视图xamarin.forms中selecteditem的背景色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆