[英]Xamarin.Forms iOS listview selected item color
I need to change the selected item color of a listview in my Xamarin.Forms app. 我需要在Xamarin.Forms应用程序中更改列表视图的选定项目颜色。 So I created a custom renderer... 所以我创建了一个自定义渲染器...
PCL C#: PCL C#:
public class DarkViewCell : ViewCell {}
PCL XAML: PCL XAML:
<ListView>
<ListView.ItemTemplate>
<DataTemplate>
<local:DarkViewCell>
<ViewCell.View>
... Stuff ...
</ViewCell.View>
</local:DarkViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
iOS 的iOS
public class DarkViewCellRenderer : ViewCellRenderer
{
private UIView bgView;
public override UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv)
{
var cell = base.GetCell(item, reusableCell, tv);
cell.BackgroundColor = UIColor.Black;
cell.TextLabel.TextColor = UIColor.White;
if (bgView == null)
{
bgView = new UIView(cell.SelectedBackgroundView.Bounds);
bgView.Layer.BackgroundColor = UIColor.FromRGB(48,48,48).CGColor;
bgView.Layer.BorderColor = UIColor.FromRGB(48, 48, 48).CGColor;
bgView.Layer.BorderWidth = 2.0f;
}
cell.SelectedBackgroundView = bgView;
return cell;
}
}
But is not working. 但是没有用。 I also tried to change the SelectionStyle but nothing... 我也尝试更改SelectionStyle,但是什么也没做...
EDIT 编辑
In a new project it works. 在一个新项目中,它可以工作。 And the code is the same 和代码是一样的
I'm not 100% sure but, I removed 我不确定100%,但是我删除了
<x:Arguments>
<ListViewCachingStrategy>RecycleElement</ListViewCachingStrategy>
</x:Arguments>
and it started working. 它开始工作。
Try setting: 尝试设置:
public override UITableViewCell GetCell(Cell item, UITableView tv)
{
var cell = base.GetCell(item, tv);
cell.SelectedBackgroundView = new UIView() { BackgroundColor = UIColor.Black };
return cell;
}
Update: I just used your code in a dummy project and it works as it should. 更新:我只是在一个虚拟项目中使用了您的代码,它可以正常工作。 Did you add the assembly attribute to register the custom renderer? 是否添加了Assembly属性以注册自定义渲染器?
[assembly: ExportRenderer(typeof(DarkViewCell), typeof(DarkViewCellRenderer))]
namespace MyProject.iOS
{
public class DarkViewCellRenderer : ViewCellRenderer
{
}
}
I had a similar issue recently, I found the best way to set the background color of a cell is to use the cells ContentView
最近我遇到了类似的问题,我发现设置单元格背景色的最佳方法是使用单元格ContentView
Try using the following in your GetCell
method 尝试在GetCell
方法中使用以下内容
cell.ContentView.BackgroundColor = UIColor.Black;
Follow below code it's working for me 按照下面的代码为我工作
public override UITableViewCell GetCell(Cell item, UITableView tv)
{
var cell = base.GetCell(item, tv);
cell.SelectedBackgroundView = new UIView {
BackgroundColor = UIColor.DarkGray,
};
return cell;
}
Below link is useful for you 以下链接对您有用
Xamarin.Forms ListView: Set the highlight color of a tapped item Xamarin.Forms ListView:设置点击项目的突出显示颜色
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.