Xamarin Forms 样式 - 支持多种目标类型

Nir*_*edi 6 xaml xamarin.forms xamarin.forms-styles

我最近更新到了最新的 Xamarin 表单预发行版 4.2 版本。我遇到的一个值得注意的重大变化是 - 假设我有以下风格:

    <Style x:Key="LightTextLabelStyle" TargetType="Label">
        <Setter Property="FontFamily" Value="{StaticResource TextLight}" />
        <Setter Property="FontSize" Value="15" />
        <Setter Property="TextColor" Value="{StaticResource greyishBrown}" />               
    </Style>
Run Code Online (Sandbox Code Playgroud)

在以前的版本中,跨度和标签都支持相同的目标“标签”。就像 - 这之前是有效的:

    <Label Margin="0,6,0,0">
         <Label.FormattedText>
              <FormattedString>
                    <Span Text="{Binding PriceText}" Style="{StaticResource LightTextLabelStyle}" FontSize="13" />
                     <Span Text="{Binding BidAmount, StringFormat=' {0:C0}' TargetNullValue=' Pending'}" Style="{StaticResource LightTextLabelStyle}" FontSize="13" />
              </FormattedString>
          </Label.FormattedText>
    </Label>
Run Code Online (Sandbox Code Playgroud)

Span 也支持针对 Label 的相同样式。然而现在在新版本中却没有了。

我的问题是:我们可以支持相同风格的 Label 和 Span 吗?我们不能为两者定位相同的风格吗?就像我尝试了以下但它无法编译:

    <Style x:Key="LightTextLabelStyle" TargetType="Label, Span">
        <Setter Property="FontFamily" Value="{StaticResource TextLight}" />
        <Setter Property="FontSize" Value="15" />
        <Setter Property="TextColor" Value="{StaticResource greyishBrown}" />               
    </Style>
Run Code Online (Sandbox Code Playgroud)

请帮我。不过,我可以复制粘贴样式并制作 2 种不同的样式;如果有更好的办法吗?

Nir*_*edi 4

到目前为止,最好的解决方案是为 Label 和 Span 创建两种不同的样式。早期的 Xamarin 表单支持两者相同的样式,但现在不支持。所以我最终得到了:

<Style x:Key="LightTextLabelStyle" TargetType="Label">
   <Setter Property="FontFamily" Value="{StaticResource TextLight}" />
   <Setter Property="FontSize" Value="15" />
   <Setter Property="TextColor" Value="{StaticResource greyishBrown}" />               
</Style>

<Style x:Key="LightTextSpanStyle" TargetType="Span">
   <Setter Property="FontFamily" Value="{StaticResource TextLight}" />
   <Setter Property="FontSize" Value="15" />
   <Setter Property="TextColor" Value="{StaticResource greyishBrown}" />               
</Style>
Run Code Online (Sandbox Code Playgroud)