How do I define a StaticResource of type Thickness?

JLundaJLunda USMember ✭✭

I am trying to define a StaticResource in an application-level ResourceDictionary, and I am having trouble with the following markup:

      <Thickness x:Key="RelativeLayoutPadding">
        <Setter Property="Top" Value="15" />
        <Setter Property="Bottom" Value="15" />
        <Setter Property="Left" Value="15" />
        <Setter Property="Right" Value="15" />

I know the ResourceDictionary works, because it works with other types that I am setting, such as Doubles. However, for this markup, I get the error message that the property "Top" cannot be resolved (with a XamlParse exception). I originally thought this might be because the Thickness structure may not have public static properties for its Top, Bottom, Left, and Right Doubles. However, I have seen sample code for LayoutOptions having its Alignment property defined as a StaticResource and it is unclear from the documentation as to whether or not either the Alignment property or the Top, Bottom, Left, and Right properties are static. All, however, are clearly public, so I don't understand why the Setter class should have any issue in setting these properties. Please provide assistance.



  • Luis_dmLuis_dm USMember ✭✭✭
    edited January 2017

    i hope help you...

     <OnPlatform x:Key="PaddingPlataformas"

    or also can be:

         <OnPlatform x:Key="PaddingPlataformas"

    ( Left, Top, Right, Bottom)

  • JLundaJLunda USMember ✭✭

    Luis, thank you for your response, but I am having trouble understanding how I am supposed to access the iOS/Android/WinPhone properties in a {StaticResource ...} statement. I thought it would automatically detect it if I had the following markup for my layout:
    <RelativeLayout Padding="{StaticResource PlatformPadding}"> <!-- Control markup --> </RelativeLayout>

    And this markup in my application level ResourceDictionary:
    <OnPlatform x:Key="PlatformPadding" x:TypeArguments="Thickness" iOS="15" Android="50" WinPhone="15"> </OnPlatform>

    ...but it is not applying the padding at all.

    I've got a temporary work-around of using Double's in my application-level ResourceDictionary, but I would like this to work as intended in the documentation.

  • BoNgoh.0138BoNgoh.0138 USMember

    Bump. Having the same issues here. Sometimes I get a compilation error, other times I get an InvalidProgramException in runtime that declares it has 'Invalid IL code' in the xaml...whatever that means.

  • MickMcCuenMickMcCuen USMember

    This is working for me:

    <OnPlatform x:Key="SpacingDimen" x:TypeArguments="Thickness" iOS="20,0,20,20" Android="20,0,20,20" WinPhone="0"> </OnPlatform>

    Then to use it:

    <Setter Property="Margin" Value="{DynamicResource SpacingDimen}"/>

    I believe the key is setting is it as "DynamicResource" not "StaticResource".

  • alexmartinezalexmartinez USMember ✭✭

    Hi there,

    The iOS, Android and WinPhone properties of the OnPlatform element are deprecated… You should use "On" subelement to specify each specific value. For instance:

    <OnPlatform x:Key="OuterPadding" x:TypeArguments="Thickness">
         <On Platform="Android">16</On>
         <On Platform="iOS">20</On>
         <On Platform="WinPhone">24</On>

    Here is the @JamesMontemagno 's post explaining the change.

Sign In or Register to comment.