Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I include an OnPlatform in a style resource?

I have this style:

       <Style x:Key="pointButton" TargetType="Button">
            <Setter Property="BorderColor" Value="#999999"/>
            <Setter Property="BorderWidth" Value="1" />
            <Setter Property="TextColor" Value="#AAAAAA" />
            <Setter Property="FontAttributes" Value="Bold" />
            <Setter Property="HorizontalOptions" Value="FillAndExpand" />
            <Setter Property="VerticalOptions" Value="StartAndExpand" />
            <Setter Property="Margin" Value="10,10,10,10" />
            <Setter Property="HeightRequest" Value="40" />
        </Style>

I would like to include this. Is it possible?

<Button.FontSize>
   <OnPlatform x:TypeArguments="x:Double" iOS="25" Android="20" />
</Button.FontSize>
like image 315
Alan2 Avatar asked Aug 02 '17 14:08

Alan2


2 Answers

With the new Xamarin OnPlatform specification is possible to define the platform styles directly in the style tag:

<Style x:Key="buttonStyle" TargetType="Button">
  <Setter Property="FontSize">
    <OnPlatform x:TypeArguments="x:Double">
      <On Platform="Android">40</On>
        <On Platform="iOS">10</On>
     </OnPlatform>
   </Setter>
</Style>
like image 198
StefanoV Avatar answered Oct 28 '22 06:10

StefanoV


Define every property as OnPlatform

<OnPlatform x:Key="MyFontSize" x:TypeArguments="x:Double" iOS="14" Android="14" WinPhone="14" />

and refer it in yout button style like

<Style x:Key="MyButtonStyle" TargetType="{x:Type Button}">
    <Setter Property="FontSize" Value="{StaticResource MyFontSize}" />
</Style>
like image 21
Longa Avatar answered Oct 28 '22 06:10

Longa