Nathan
Nathan

Reputation: 7709

Xamarin.Forms: How to set values in Style only on specific platform

In a style in xamarin forms (defined in xaml) I can define values platform specific, like this:

<OnPlatform x:TypeArguments="Font" Android="30" iOS="40" WinPhone="20" x:Key="TitleFontSize" />

<Style x:Key="MyTitleLabel" TargetType="Label"">
    <Setter Property="Font" Value="{StaticResource TitleFontSize}" />
</Style>

Can I also define the style in a way, so that the value of "Font" is kept to the default (not set at all) for some plarforms?

Something like this:

<Style x:Key="MyTitleLabel" TargetType="Label"">
    <OnPlatform>
    <OnPlatform.Android>
        <Setter Property="Font" Value="30" />
    </OnPlatform.Androud>
    </OnPlatform>
</Style>

Upvotes: 3

Views: 2017

Answers (2)

Sharada
Sharada

Reputation: 13601

There is no way avoid setting a value - if no value is specified it will default to default(T).

But, with the latest XF version (> 2.4.0.282) there is a Default property available that you can use to specify the fallback value.

<!-- specify default value here -->
<OnPlatform x:TypeArguments="x:Double" Default="25">
    <OnPlatform.Android>
        <Setter Property="Font" Value="30"/>
    </OnPlatform.Android>
</OnPlatform>

Upvotes: 3

Rudy Spano
Rudy Spano

Reputation: 1421

This should work:

<Style x:Key="MyTitleLabel" TargetType="Label"">
   <Setter Property="Font">
     <Setter.Value>                   
       <OnPlatform x:TypeArguments="x:Double">
          <On Platform="Android">30</On>
          <!--?<On Platform="iOS">20</On>-->
       </OnPlatform>
     </Setter.Value>
   </Setter>
</Style>

Upvotes: 2

Related Questions