我需要在我的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,但什么都没有.
编辑
在一个新的项目中,它是有效的。代码是相同的
发布于 2017-07-14 13:36:17
我不是百分之百确定,但是,我删除了
<x:Arguments>
<ListViewCachingStrategy>RecycleElement</ListViewCachingStrategy>
</x:Arguments>开始起作用了。
发布于 2017-07-12 15:17:01
试着设置:
public override UITableViewCell GetCell(Cell item, UITableView tv)
{
var cell = base.GetCell(item, tv);
cell.SelectedBackgroundView = new UIView() { BackgroundColor = UIColor.Black };
return cell;
}更新:--我刚刚在一个虚拟项目中使用了您的代码,它可以正常工作。添加程序集属性以注册自定义呈现程序吗?
[assembly: ExportRenderer(typeof(DarkViewCell), typeof(DarkViewCellRenderer))]
namespace MyProject.iOS
{
public class DarkViewCellRenderer : ViewCellRenderer
{
}
}发布于 2017-07-12 15:33:31
最近我遇到了一个类似的问题,我发现设置一个单元格的背景色的最好方法是使用单元格ContentView。
尝试在GetCell方法中使用以下内容
cell.ContentView.BackgroundColor = UIColor.Black;https://stackoverflow.com/questions/45061210
复制相似问题