5

我最近更新到最新的 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>

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

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

针对 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>

请帮我。我可以复制粘贴样式并制作两种不同的样式;如果有更好的方法?

4

2 回答 2

3

到目前为止,最好的解决方案是为标签和跨度创建两种不同的样式。早期的 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>
于 2019-07-23T13:54:02.843 回答
0

当我在 Xamarin.forms 4.2 版中构建您的代码时,我可以重现您的问题,但它在 Xamarin.Forms 4.1 版中运行良好,因此我已向 Microsoft 支持团队报告了此问题。

但是现在你可以看看下面的代码来暂时解决你的问题。

 <Label Margin="0,6,0,0" Style="{StaticResource LightTextLabelStyle}">
            <Label.FormattedText>
                <FormattedString>
                    <Span FontSize="20" Text="this is test, please take a look!" />
                    <Span FontSize="20" Text="hello world!" />
                </FormattedString>
            </Label.FormattedText>
        </Label>
于 2019-07-18T02:57:21.930 回答