【问题标题】:Xamarin Forms implicit / explicit styles ... inheritanceXamarin Forms 隐式/显式样式...继承
【发布时间】:2017-01-11 19:25:33
【问题描述】:

我正在尝试考虑像 CSS 之类的 Xamarin (XAML) 样式,但发现它并不完全相同。例如,如果我希望所有标签都是白色的,但某些标签也是粗体,从逻辑上讲,我认为这是可行的。

    <ResourceDictionary>
        <Style TargetType="Label">
            <Setter Property="TextColor" Value="White" />
        </Style>
        <Style x:Key="LargeLabel" TargetType="Label">
            <Setter Property="FontAttributes" Value="Bold" />
        </Style>
    </ResourceDictionary>

在页面中...

    <Label Text="Good Morning David!" Style="{StaticResource LargeLabel}" />

但是,这给了我一个加粗的黑色标签。我希望它是白色的。

因此,经过一些研究,建议 BasedOn 属性可以提供帮助。该技术是创建隐式样式所基于的显式样式。这样所有标签都变白了。然后进一步的显式样式可以基于同一个父级。

    <ResourceDictionary>
        <Style TargetType="Label" x:Key="DefaultLabelStyle">
            <Setter Property="TextColor" Value="White" />
        </Style>
        <Style x:Key="LargeLabel" TargetType="Label" BasedOn="DefaultLabelStyle">
            <Setter Property="FontAttributes" Value="Bold" />
        </Style>
        <Style TargetType="Label" BasedOn="DefaultLabelStyle" />
    </ResourceDictionary>

在页面中也是一样的东西……

    <Label Text="Good Morning David!" Style="{StaticResource LargeLabel}" />

但是,这仍然会导致黑色粗体标签而不是粗体白色标签。

有没有办法在 XAML/Xamarin Forms 中进行这项工作?

【问题讨论】:

    标签: xaml xamarin.forms


    【解决方案1】:

    BasedOn 必须是 StaticResource 每个 Xamarin Documentation

    这就是我在项目中的做法:

    <ResourceDictionary>
        <Style TargetType="Label" x:Key="DefaultLabelStyle">
            <Setter Property="TextColor" Value="White" />
        </Style>
        <Style x:Key="LargeLabel" TargetType="Label" BasedOn="{StaticResource DefaultLabelStyle}>
            <Setter Property="FontAttributes" Value="Bold" />
        </Style>
    </ResourceDictionary>
    

    My working source code example

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-03
      相关资源
      最近更新 更多