如何更改 Xamarin.UWP 中 TableView TableSection 中的 TextSize 和大小写

Posted

技术标签:

【中文标题】如何更改 Xamarin.UWP 中 TableView TableSection 中的 TextSize 和大小写【英文标题】:How to change the TextSize and Casing in TableView TableSection in Xamarin.UWP 【发布时间】:2021-02-09 01:48:24 【问题描述】:

我有一个由 Xamarin TableView 制作的 CustomTableView 控件。我使用自定义渲染器来更改 androidios 的 TextSize 和 Boldness,代码灵感来自 post。

我想为 UWP 做同样的事情,但我不知道如何实现这一点。具体来说,我想让 TableSections 中的文本更大,并使此文本也以大写字母开头。任何关于我如何实现这一目标的想法都将受到高度赞赏。

【问题讨论】:

【参考方案1】:

如何在 Xamarin.UWP 中更改 TableView TableSection 中的 TextSize 和大小写

请检查此 code line,Xamarin 将 TextBlock 放置在 TableSection DataTemplate 中。如果要编辑属性,可以通过在 UWP 项目中的 App.Xaml 文件中添加 DataTemplate 来实现。如果要编辑FontSize,可以使用下面的代码。 (注意 FontSize 属性)

<Application.Resources>
    <DataTemplate x:Key="TableSectionOne">
        <TextBlock
            Margin="0,20,0,0"
            FontSize="55"
            Foreground="Binding TextColor, Converter=StaticResource ColorConverter, ConverterParameter=DefaultTextForegroundThemeBrush"
            Style="ThemeResource SubtitleTextBlockStyle"
            Text="Binding Title, Converter=StaticResource LowerConverter"
            Visibility="Binding Text, RelativeSource=RelativeSource Mode=Self, Converter=StaticResource CollapseWhenEmpty" />
    </DataTemplate>
</Application.Resources>

在您的 customTableView 的 UWP 渲染器中,您可以手动设置 listview.GroupStyle.FirstOrDefault().HeaderTemplate,如下所示。

class CustomTableViewRender : TableViewRenderer

    protected override void OnElementChanged(ElementChangedEventArgs<TableView> e)
    
        base.OnElementChanged(e);

        if(Control != null)
        
            var listview = Control as Windows.UI.Xaml.Controls.ListView;
            listview.GroupStyle.FirstOrDefault().HeaderTemplate = (Windows.UI.Xaml.DataTemplate)Windows.UI.Xaml.Application.Current.Resources["TableSectionOne"];
           
        
       

很遗憾,TableSection 不支持继承,因此我们无法为其扩展依赖属性。

至于Title 的大小写,您可以简单地从Text 中删除Converter=StaticResource LowerConverter,并将TableViewTitle 设置为您喜欢的任何大小写的字符串不转换为小写。所以它最终看起来像这样:

<Application.Resources>
    <DataTemplate x:Key="TableSectionOne">
        <TextBlock
            Margin="0,20,0,0"
            FontSize="55"
            Foreground="Binding TextColor, Converter=StaticResource ColorConverter, ConverterParameter=DefaultTextForegroundThemeBrush"
            Style="ThemeResource SubtitleTextBlockStyle"
            Text="Binding Title"
            Visibility="Binding Text, RelativeSource=RelativeSource Mode=Self, Converter=StaticResource CollapseWhenEmpty" />
    </DataTemplate>
</Application.Resources>

【讨论】:

非常有帮助,我确实进行了一些调整以使其正常工作以及修复文本的大小写,但这绝对让我走上了正确的轨道。谢谢!我将编辑答案以包含我所做的更改并将其标记为正确

以上是关于如何更改 Xamarin.UWP 中 TableView TableSection 中的 TextSize 和大小写的主要内容,如果未能解决你的问题,请参考以下文章