Forum Xamarin.Forms

XForms 1.3.2: Can OnPlatform be used with ResourceDictionary.Styles?

Does anyone of if you can specify OnPlatform declarations when using Styles? I've tried a few different things but can't get it to work. My last attempt looked liked this:

`

<ResourceDictionary>
    <Style x:Key="ButtonStyle" TargetType="Button">
        <Setter Property="VerticalOptions" Value="Center" />
        <Setter Property="HorizontalOptions" Value="Center" />

        <Style.Setter.Property.HeightRequest>
            <OnPlatform x:TypeArguments="x:Double" iOS="94" Android="100" />
        </Style.Setter.Property.HeightRequest>

        <Style.Setter.Property.WidthRequest>
            <OnPlatform x:TypeArguments="x:Double" iOS="94" Android="100" />
        </Style.Setter.Property.WidthRequest>
    </Style>
</ResourceDictionary>

`

Posts

  • PaulHazlettPaulHazlett USMember ✭✭

    Maybe the only way this works is to create an iOS and an Android style and then use OnPlatform with the buttons to choose which style to apply.

  • PaulHazlettPaulHazlett USMember ✭✭

    That makes complete sense. I'm not sure why I couldn't figure that out :/

    Thanks!

  • SteeveLeDreauSteeveLeDreau FRMember ✭✭

    It's not working with font size, is it ?

  • CodeGrueCodeGrue USMember ✭✭
    edited June 2017

    This worked for me to express a platform specific color resource from:

    <ResourceDictionary>
        <Color x:Key="Accent">#4d4d4d</Color>
    </ResourceDictionary>
    

    To:

    <ResourceDictionary>
            <OnPlatform x:Key="Accent" x:TypeArguments="Color">
                <On Platform="iOS">#000000</On>
                <On Platform="Android">#4d4d4d</On>
            </OnPlatform>
    </ResourceDictionary>
    
  • john82john82 ITMember ✭✭✭
    edited March 2018

    I can't make it works!

    UPDATE:
    Ok never mind, I've figured it out. First I was missing the "s" in the TypeArguments property, then, I forgot to populate my with values. It now works!


    It throws an exception like this:
    "Xamarin.Forms.Xaml.XamlParseException: Position 4:6. Property Resources is null or is not IEnumerable"

            <OnPlatform x:Key="LatoRegular" x:TypeArguments="x:String">
                <On Platform="iOS">Lato Regular"</On>
                <On Platform="Android">Lato-Regular.ttf#Lato Regular"</On>
            </OnPlatform>
    

    How I can achieve this?

  • teapengteapeng USMember ✭✭

    @SuarezDiego said:
    I could make it work with OnPlatform.
    The OnPlatform should be on the Setter.Value like this:

    <Style x:Key="ButtonStyle" TargetType="Button">
            <Setter Property="VerticalOptions" Value="Center" />
            <Setter Property="HorizontalOptions" Value="Center" />
    
            <Setter Property="BackgroundColor">
              <Setter.Value>
                <OnPlatform x:TypeArguments="Color" iOS="Red" Android="Green" />
              </Setter.Value>
            </Setter>
    
            <Setter Property="HeightRequest">
              <Setter.Value>
                <OnPlatform x:TypeArguments="x:Double" iOS="94" Android="100" />
              </Setter.Value>
            </Setter>
    
            <Setter Property="WidthRequest">
              <Setter.Value>
                <OnPlatform x:TypeArguments="x:Double" iOS="94" Android="100" />
              </Setter.Value>
            </Setter>
    </Style>
    

    Good answer, thanks.

Sign In or Register to comment.