有没有办法在表视图Xamarin窗体中禁用视图单元格突出显示?



我正在尝试使用TableView viewCell制作表单。一切正常,但我想禁用该视图单元格上的突出显示功能。我在互联网上找不到任何内容,Xamarin文档中也没有与此相关的内容。

任何帮助将不胜感激。

我相信您正在谈论的突出显示功能是单元格或列表/表视图用于呈现 Xamarin.Forms TableView/ViewCell 的本机控件的一部分,因此您必须创建自定义呈现器,或使用效果来修改本机控件上的属性。我个人更习惯于自定义渲染器而不是效果器,因此这就是我将在以下示例中使用的内容。

我可以给你一些iOS和Android的例子,但我必须深入研究UWP以获得UWP平台的解决方案。

在 Android 上,您可以在用于呈现 Xamarin.Forms TableView 的本机 Android ListView 上将突出显示颜色设置为透明。只需将 C# 代码文件添加到 Android 项目并添加以下代码,并根据解决方案更改命名空间:

using Android.Content;
using TableViewSamples.Droid;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
[assembly: ExportRenderer(typeof(TableView), typeof(CustomTableViewRenderer))]
namespace TableViewSamples.Droid
{
class CustomTableViewRenderer : TableViewRenderer
{
public CustomTableViewRenderer(Context context) : base(context) { }
protected override void OnElementChanged(ElementChangedEventArgs<TableView> e)
{
base.OnElementChanged(e);
if (Control != null)
{
// This sets the highlight color to transparent for all cells in the Android native ListView:
Control.Selector = new Android.Graphics.Drawables.ColorDrawable(Android.Graphics.Color.Transparent);
}
}
}
}

在 iOS 上,突出显示颜色是在单元格类上设置的,因此您需要为 ViewCell 而不是 TableView 创建自定义呈现器:

using TableViewSamples.iOS;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
[assembly: ExportRenderer(typeof(ViewCell), typeof(CustomViewCellRenderer))]
namespace TableViewSamples.iOS
{
class CustomViewCellRenderer : ViewCellRenderer
{
public override UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv)
{
var cell = base.GetCell(item, reusableCell, tv);
// This sets the highlight color to transparent for each UITableViewCell in the iOS native UITableView:
cell.SelectionStyle = UITableViewCellSelectionStyle.None;
return cell;
}
}
}

请注意,这些将替换 Xam.Forms Android 应用中所有 Xam.Forms TableView 状态的默认呈现器,以及 Xam.Forms iOS 应用中的所有 ViewCell 实例的默认呈现器。如果你只需要对某些 TableView 和 ViewCell 执行此操作,那么你需要对 TableView 和 ViewCell 进行子类化,并使渲染器引用 ExportRenderer 属性中的这些子类,例如:

[assembly: ExportRenderer(typeof(ViewCellSubClass), typeof(CustomViewCellRenderer))]

[assembly: ExportRenderer(typeof(TableViewSubClass), typeof(CustomTableViewRenderer))]

最新更新