简体   繁体   English

如何设置TextColor Xamarin.Forms TableSection?

[英]How to set TextColor Xamarin.Forms TableSection?

I am a fan of doing as much as possible in xaml, I have aTableView` with a TableSection. 我是在xaml中尽可能多地做的粉丝,我有一个带有TableSection的表格。

<TableView Intent="Menu">
     <TableRoot>
          <TableSection Title="Test Section" TextColor="#FFFFFF">
                <TextCell Text="Test Item" TextColor="#FFFFFF"/>
          </TableSection>
     </TableRoot>
</TableView>

For TextCell TextColor="#FFFFFF" seems to work, however whenever I use this attribute on a TableSection I get this: 对于TextCell TextColor="#FFFFFF"似乎有效,但每当我在TableSection上使用此属性时,我得到:

An unhandled exception occurred.

Is it possible to change the color of the TableSection with xaml? 是否可以使用xaml更改TableSection的颜色?

Custom Renderers! 自定义渲染器! I have two blog posts on this here: 我在这里有两篇博文:

Android: Xamarin.Forms TableView Section Custom Header on Android Android:Android上的Xamarin.Forms TableView部分自定义标题

iOS: Xamarin.Forms TableView Section Custom Header on iOS iOS:iOS上的Xamarin.Forms TableView部分自定义标题

Basically, create a custom view that inherits TableView , then custom renderers that implement custom TableViewModelRenderer . 基本上,创建一个继承TableView的自定义视图,然后创建实现自定义TableViewModelRenderer自定义渲染器。 From there you can override methods to get the header view for the section title. 从那里,您可以覆盖方法以获取节标题的标题视图。

Here's what that might look like for Android: 以下是Android的外观:

public class ColoredTableViewRenderer : TableViewRenderer
{

    protected override TableViewModelRenderer GetModelRenderer(Android.Widget.ListView listView, TableView view)
    {
        return new CustomHeaderTableViewModelRenderer(Context, listView, view);
    }

    private class CustomHeaderTableViewModelRenderer : TableViewModelRenderer
    {
        private readonly ColoredTableView _coloredTableView;

        public CustomHeaderTableViewModelRenderer(Context context, Android.Widget.ListView listView, TableView view) : base(context, listView, view)
        {
            _coloredTableView = view as ColoredTableView;
        }

        public override Android.Views.View GetView(int position, Android.Views.View convertView, ViewGroup parent)
        {
            var view = base.GetView(position, convertView, parent);

            var element = GetCellForPosition(position);

            // section header will be a TextCell
            if (element.GetType() == typeof(TextCell))
            {
                try
                {
                    // Get the textView of the actual layout
                    var textView = ((((view as LinearLayout).GetChildAt(0) as LinearLayout).GetChildAt(1) as LinearLayout).GetChildAt(0) as TextView);

                    // get the divider below the header
                    var divider = (view as LinearLayout).GetChildAt(1);

                    // Set the color
                    textView.SetTextColor(_coloredTableView.GroupHeaderColor.ToAndroid());
                    textView.TextAlignment = Android.Views.TextAlignment.Center;
                    textView.Gravity = GravityFlags.CenterHorizontal;
                    divider.SetBackgroundColor(_coloredTableView.GroupHeaderColor.ToAndroid());
                }
                catch (Exception) { }
            }

            return view;
        }
    }
}

And the on iOS: 在iOS上:

public class ColoredTableViewRenderer : TableViewRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<TableView> e)
    {
        base.OnElementChanged(e);
        if (Control == null)
            return;

        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
            };
        }
    }
}

According to the official documentation for TableSection you are out of luck. 根据TableSection的官方文档,你运气不好。 I'm afraid you would have to write a custom renderer for a subclass of the TableSection class and expose an extra property of type Xamarin.Forms.Color . 恐怕你会写的子类定制呈现TableSection和暴露类型的额外的属性Xamarin.Forms.Color Then you would be able to set the color from XAML. 然后,您就可以从XAML设置颜色。

You can set this color in the base theme (may apply to other widgets too) 您可以在基本主题中设置此颜色(也可以应用于其他小部件)

In /Resources/values/styles.xml /Resources/values/styles.xml中

<style name="MainTheme.Base" parent="Theme.AppCompat.Light">
    <item name="colorAccent">#434857</item>

For Individual Section Titles, the TextColor property now seems to work correctly: 对于Individual Section Titles, TextColor属性现在似乎正常工作:

<TableView Intent="Settings">
    <TableRoot>
        <TableSection Title="App Settings" TextColor="Red">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM