【问题标题】:Xamarin Forms Styles - Support multiple target typesXamarin 表单样式 - 支持多种目标类型
【发布时间】:2019-11-26 18:06:24
【问题描述】:

我最近更新到了最新的 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。但是现在在新版本中它没有。

我的问题是: 我们可以同时支持具有相同样式的 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>

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

【问题讨论】:

    标签: xaml xamarin.forms xamarin.forms-styles


    【解决方案1】:

    到目前为止,最好的解决方案是为标签和跨度创建两种不同的样式。早期的 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>
    

    【讨论】:

      【解决方案2】:

      当我在 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>
      

      【讨论】:

      • 这会将所有跨度格式化为相同的样式,但是基于跨度的样式属性(如您指定的 FontSize)会覆盖这些属性。我认为这不是最好的解决方案。
      • 我认为目前最好的解决方案是为 Label 和 Span 保留 2 种不同的样式并使用它们。
      • @Mirmal,你是对的,我已经向微软支持团队报告了这个问题,所以请稍等。这里是问题链接:github.com/xamarin/Xamarin.Forms/issues/6907
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-06
      • 1970-01-01
      • 2020-01-07
      • 2019-09-16
      • 2020-10-26
      相关资源
      最近更新 更多