Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin Forms Styles - Support multiple target types

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?

like image 823
Nirmal Subedi Avatar asked Oct 16 '22 12:10

Nirmal Subedi


1 Answers

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>
like image 123
Nirmal Subedi Avatar answered Oct 21 '22 00:10

Nirmal Subedi