XAML Xamarin自定义表视图标题

cyej8jka  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(157)

我想在表视图节标题的标题中添加一个按钮,iidoEe.一个加号按钮,经过研究发现,要做到这一点,你必须创建一个自定义标题。我无法找到如何做到这一点。
如何在xamarin中为表视图部分创建自定义标题?
我也在使用Xaml和C#

p3rjfoxz

p3rjfoxz1#

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

相关问题