首页 > 解决方案 > Xamarin:如何覆盖所有标签的标签样式类?

问题描述

在 nuget 包中有一个 Xaml 文件,其中包含:

<Style x:Key="LabelBase" TargetType="Label">
    <Setter Property="TextColor" Value="Green" />
</Style>

<Style
    BasedOn="{StaticResource LabelBase}"
    Class="Primary"
    TargetType="Label">
</Style>

在当前项目 App.xaml 我有:

<Style TargetType="Label">
   <Setter Property="TextColor" Value="Red" />
</Style>

ResourceDictionary 具有MergedWith="XAMLFILE"nuget .xaml 文件 ResourceDictionary。

在页面的 AbsoluteLayout 我有一个标签

<Label
    Grid.Row="0"
    FontSize="45"
    HorizontalTextAlignment="Center"
    StyleClass="Primary"
    Text="Text Value"
    VerticalOptions="Center" />

如何在我的项目 App.Xaml 中覆盖 Primary StyleClass 以使文本颜色继承红色而不创建新的 styleclass?

标签: xmlxamarinxamarin.formsxamarin.android

解决方案


如果您希望将该主题应用于所有标签,只需删除该类,它应该可以工作:

<Style
BasedOn="{StaticResource LabelBase}"
TargetType="Label">
</Style>

请注意您没有密钥并且样式在 APP.xaml 中

祝你好运

随意恢复。


推荐阅读