![](/img/trans.png)
[英]Xamarin.Forms ListView with CustomViewCell disable Selected Item Color
[英]Xamarin.Forms iOS listview selected item color
我需要在Xamarin.Forms应用程序中更改列表视图的选定项目颜色。 所以我创建了一个自定义渲染器...
PCL C#:
public class DarkViewCell : ViewCell {}
PCL XAML:
<ListView>
<ListView.ItemTemplate>
<DataTemplate>
<local:DarkViewCell>
<ViewCell.View>
... Stuff ...
</ViewCell.View>
</local:DarkViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
的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;
}
}
但是没有用。 我也尝试更改SelectionStyle,但是什么也没做...
编辑
在一个新项目中,它可以工作。 和代码是一样的
我不确定100%,但是我删除了
<x:Arguments>
<ListViewCachingStrategy>RecycleElement</ListViewCachingStrategy>
</x:Arguments>
它开始工作。
尝试设置:
public override UITableViewCell GetCell(Cell item, UITableView tv)
{
var cell = base.GetCell(item, tv);
cell.SelectedBackgroundView = new UIView() { BackgroundColor = UIColor.Black };
return cell;
}
更新:我只是在一个虚拟项目中使用了您的代码,它可以正常工作。 是否添加了Assembly属性以注册自定义渲染器?
[assembly: ExportRenderer(typeof(DarkViewCell), typeof(DarkViewCellRenderer))]
namespace MyProject.iOS
{
public class DarkViewCellRenderer : ViewCellRenderer
{
}
}
最近我遇到了类似的问题,我发现设置单元格背景色的最佳方法是使用单元格ContentView
尝试在GetCell
方法中使用以下内容
cell.ContentView.BackgroundColor = UIColor.Black;
按照下面的代码为我工作
public override UITableViewCell GetCell(Cell item, UITableView tv)
{
var cell = base.GetCell(item, tv);
cell.SelectedBackgroundView = new UIView {
BackgroundColor = UIColor.DarkGray,
};
return cell;
}
以下链接对您有用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.