Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# listview xamarin.forms中selecteditem的背景色_C#_Xamarin_Xamarin.forms_Xamarin.forms.listview - Fatal编程技术网

C# listview xamarin.forms中selecteditem的背景色

C# listview xamarin.forms中selecteditem的背景色,c#,xamarin,xamarin.forms,xamarin.forms.listview,C#,Xamarin,Xamarin.forms,Xamarin.forms.listview,在Xamarin.forms中使用ListView时,在选择项目时,该项目仍然与背景颜色相当难看的项目相同。我可以禁用此功能吗 这是我的密码: 风险投资 这可以通过使用自定义渲染器来实现 iOS自定义渲染器 在iOS上编辑SelectionStyle属性 下面是一个将UITableViewCellSelectionStyle设置为None的示例 using System; using UIKit; using Xamarin.Forms; using Xamarin.Forms.Platf

在Xamarin.forms中使用ListView时,在选择项目时,该项目仍然与背景颜色相当难看的项目相同。我可以禁用此功能吗

这是我的密码:


风险投资

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

iOS自定义渲染器 在iOS上编辑
SelectionStyle
属性

下面是一个将
UITableViewCellSelectionStyle
设置为
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自定义渲染器
  • 创建一个新的可绘制视图,
    ViewCellBackground.xml
    并将其保存到
    Resources
    drawable
    文件夹:

    <?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>
    
  • 编辑:删除了没有自定义渲染器的实现


    示例Xamarin.Forms列表视图应用程序
    使用系统;
    使用System.Collections.Generic;
    使用Xamarin.Forms;
    名称空间ListViewSample
    {
    公共类CustomViewCell:ViewCell
    {
    公共CustomViewCell()
    {
    视图=新标签
    {
    Text=“你好,世界”
    };
    }
    }
    公共类ListViewContentPage:ContentPage
    {
    公共ListViewContentPage()
    {
    var itemSourceList=新列表();
    添加(新的CustomViewCell());
    添加(新的CustomViewCell());
    var listView=new listView();
    listView.ItemTemplate=新数据模板(typeof(CustomViewCell));
    listView.ItemsSource=itemSourceList;
    listView.SeparatorVisibility=SeparatorVisibility.None;
    内容=列表视图;
    }
    }
    公共类应用程序:应用程序
    {
    公共应用程序()
    {
    //应用程序的根页面
    主页=新导航页(新ListViewContentPage());
    }
    }
    }
    
    OnListViewTextCellTapped这是一种自定义方法吗?是-很抱歉!我刚刚更新了我的答案以包含OnListViewTextCellTapped方法。好的。使用listview时,我可以访问所选项目,这是否适用于此处?抱歉,我不理解您的问题。你能说得更具体一点吗?如果我正常处理listview选中的项目,我可以访问listview.selecteditem…..当使用你的方法时,我如何获得选中的项目??
    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;
            }
        }
    }
    
    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());
            }
        }
    }