Xamarin Forms Styles - Support multiple target types

2.7k Views Asked by At

I have recently updated to very latest Xamarin forms pre release 4.2 version. One notable breaking change I have encounter is - Say I have the following Style:

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

In previous versions, same target "Label" was supported for both Span and Labels. Like - this was working before:

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

Same style targeted for Label was support on Span as well. However now in new version it doesn't.

My Question is: Can we support Both Label and Span with same style? Can we not target same style for both? Like I tried the following but it doesn't compile:

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

Please help me. I can copy paste the style and make 2 different styles however; if there's a some better way?

2

There are 2 best solutions below

0
NKR On BEST ANSWER

So far the best solution is to create two different Styles for Label and Span. Earlier Xamarin forms supported same style for both but not now. So I ended up with having:

<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>
3
Cherry Bu - MSFT On

I can reproduce your issue when I build your code at Xamarin.forms version 4.2, but it works fine at Xamarin.Forms version 4.1, so I have reported this issue for Microsoft support team.

But now you can look at the following code to temporarily solve your problem.

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