c# - Xamarin 自定义表视图标题
问题描述
我想在表格视图部分标题的标题中添加一个按钮,即加号按钮,经过研究发现,要做到这一点,您必须创建一个自定义标题。我不知道该怎么做。
如何在 xamarin 中为表格视图部分创建自定义标题?
我也在使用 Xaml 和 C#
解决方案
请参阅这些博客文章: https ://alexdunn.org/2017/03/21/xamarin-tips-xamarin-forms-ios-custom-tableview-section-titles/ https://alexdunn.org/2017/03/21 /xamarin-tips-xamarin-forms-android-custom-tableview-section-titles/
他们描述了使用 TableView 的自定义渲染器来自定义节标题。iOS版本:
在共享代码中:
public partial class ColoredTableView : TableView
{
public static BindableProperty GroupHeaderColorProperty = BindableProperty.Create("GroupHeaderColor", typeof(Color), typeof(ColoredTableView), Color.White);
public Color GroupHeaderColor
{
get
{
return (Color)GetValue(GroupHeaderColorProperty);
}
set
{
SetValue(GroupHeaderColorProperty, value);
}
}
public ColoredTableView()
{
InitializeComponent();
}
}
在 iOS 项目中:
[assembly: ExportRenderer(typeof(ColoredTableView),typeof(ColoredTableViewRenderer))]
namespace YOUR_IOS_NAMESPACE
{
public class ColoredTableViewRenderer : TableViewRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<TableView> e)
{
base.OnElementChanged(e);
if (Control == null)
return;
var tableView = Control as UITableView;
var coloredTableView = Element as ColoredTableView;
tableView.WeakDelegate = new CustomHeaderTableModelRenderer(coloredTableView);
}
private class CustomHeaderTableModelRenderer : UnEvenTableViewModelRenderer
{
private readonly ColoredTableView _coloredTableView;
public CustomHeaderTableModelRenderer(TableView model) : base(model)
{
_coloredTableView = model as ColoredTableView;
}
public override UIView GetViewForHeader(UITableView tableView, nint section)
{
return new UILabel()
{
Text = TitleForHeader(tableView, section),
TextColor = _coloredTableView.GroupHeaderColor.ToUIColor(),
TextAlignment = UITextAlignment.Center
};
}
}
}
}
但是我认为使用 ListView 或制作自己的部分(即一个没有为 TableView 设置标题的部分,然后使用单元格可能是最简单的方法。为此,只需定义一个没有标题的部分并为这些部分使用 ViewCells :
<TableView Intent="Data"
HasUnevenRows="true" >
<TableRoot>
<TableSection>
<ViewCell >
<StackLayout Orientation="Horizontal">
<Label x:Name="label1" Text="Section one" />
<Button Text="Button1" Clicked="Handle_Clicked" />
</StackLayout>
</ViewCell>
<TextCell Text="TextCell"
Detail="TextCell Detail" />
<EntryCell Label="Entry Label"
Text="EntryCell Text" />
<SwitchCell Text="SwitchCell Text" />
<ViewCell >
<StackLayout Orientation="Horizontal">
<Label x:Name="label2" Text="Section Two" />
<Button Text="Button2" Clicked="Handle_Clicked" />
</StackLayout>
</ViewCell>
<TextCell Text="TextCell"
Detail="TextCell Detail" />
<EntryCell Label="Entry Label"
Text="EntryCell Text" />
<SwitchCell Text="SwitchCell Text" />
</TableSection>
</TableRoot>
</TableView>
推荐阅读
- java - persistence.xml 配置到纯 Java JDO 配置
- spring - Hibernate/JPA - 是否可以有一个具有父母 ID + 另一个字段的孩子?
- python - 用于 Excel 的 Python 自动化
- python - 为什么我无法在下拉列表中选择项目?
- git - 无法更新 git 以使用访问令牌而不是密码
- python-3.x - 如何在加载 Tkinter 窗口后让 tts (pyttsx3) 说话
- mysql - 将值从客户表访问到房间类型表
- c++ - While 条件:如何允许在非整数/双用户输入和整数限制之间进行比较
- npm - NPM 给出错误-TypeError:无法解构“未定义”或“空”的属性“stat”
- aws-lambda - 如何使用构建阶段创建的 zip 文件创建 Lambda 函数