【问题标题】:Xamarin Custom Table View HeadersXamarin 自定义表视图标题
【发布时间】:2018-11-07 22:41:32
【问题描述】:

我想在表格视图部分标题的标题中添加一个按钮,即加号按钮,经过研究发现要做到这一点,您必须创建一个自定义标题。我不知道该怎么做。

如何在 xamarin 中为表格视图部分创建自定义标题?

我也在使用 Xaml 和 C#

【问题讨论】:

  • 我假设您的意思是 Xamarin Forms TableView。它并不真正支持标题,只是部分,我认为它们不能自定义。您可以创建一个自定义 ViewCell 作为标题,或者编写一个自定义渲染器。

标签: c# xaml xamarin xamarin.forms


【解决方案1】:

查看这些博文: 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>

【讨论】:

  • 感谢您的帮助。我有点挣扎,因为添加自定义视图单元格会使它变成不同的颜色?有没有办法让视图单元格看起来与背景相同?
  • 知道如何为 UWP 执行此操作吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多