Style not honoring value set by StaticResource in Resources?

SurojitXamarinSurojitXamarin ✭✭USMember ✭✭

Hello Everyone,

I have set certain values to a property (IconWidth) in the Resources of a content view. This property is being used in a Style also contained in the same place.
However, when this is used in the style, the Image view seems to ignore this.
For example -
This doesn't work

    <OnPlatform x:TypeArguments="x:Int32" Android="32" iOS="32" Default="32" x:Key="IconWidthVertical"/>

    <Style TargetType="Image" x:Key="statusIconStyles">
    <Setter Property="WidthRequest" Value="{StaticResource Key=IconWidthVertical}" />
    <Setter Property="Aspect" Value="AspectFit"/>
    </Style>

However, this does -

    <Style TargetType="Image" x:Key="statusIconStyles">
        <Setter Property="WidthRequest" Value="32" />
        <Setter Property="Aspect" Value="AspectFit"/>
    </Style>

What am I doing wrong here? Why dosen't the StaticResource binding work?

Best Answer

  • SurojitXamarinSurojitXamarin ✭✭ US ✭✭
    Accepted Answer

    @NMackay
    I tried debugging the issue and it seems WidthRequest is a Double and I was passing it an Int32.
    The method you suggested does work. But not the runtime nor the compiler indicated an error for the type mismatch which led to confusion.

    Thanks!

Answers

  • SHUBHAMSHARMA_2927SHUBHAMSHARMA_2927 ✭✭✭ USMember ✭✭✭

    hello @SurojitXamarin
    Please Try:
    <Setter Property="WidthRequest" Value="{StaticResource IconWidthVertical}" />

  • SurojitXamarinSurojitXamarin ✭✭ USMember ✭✭

    Hi @SHUBHAMSHARMA_2927 ,

    Thanks for your suggestion; but it does not work. It behaves exactly like it would with or without the Key= qualifier.

    any other ideas?

  • NMackayNMackay mod GBInsider, University mod

    @SurojitXamarin said:
    Hi @SHUBHAMSHARMA_2927 ,

    Thanks for your suggestion; but it does not work. It behaves exactly like it would with or without the Key= qualifier.

    any other ideas?

    Which Version of Forms? Which platform are you testing on?

  • SurojitXamarinSurojitXamarin ✭✭ USMember ✭✭

    @NMackay

    I'm using forms version 3.3.0.967583 and running it on a Samsung Galaxy Tab SM-T713 Android Version 7.0 API 24 and an iPhone 6+ running IOS 12.0.1

  • NMackayNMackay mod GBInsider, University mod
    edited November 2018

    Just a tip but it's better to have images resources for different screen sizes or svg rather than enforcing a height in a style but yes I've noticed static OnPlatform resources for stuff like static resource font sizes which worked in Forms 2.5 doesn't work in the latest builds in implicit styles but they work if set on platform within the style definition

     <Style TargetType="{x:Type customControls:MyCoolButton}">
            <Setter Property="FontSize">
                <Setter.Value>
                    <OnPlatform x:TypeArguments="x:Double">
                        <On Platform="iOS">10</On>
                        <On Platform="Android,UWP">12</On>
                    </OnPlatform>
                </Setter.Value>
            </Setter>
        </Style>
    
    
  • SurojitXamarinSurojitXamarin ✭✭ USMember ✭✭

    @NMackay said:
    Just a tip but it's better to have images resources for different screen sizes or svg rather than enforcing a height in a style but yes I've noticed static OnPlatform resources for stuff like font sizes which worked in Forms 2.5 doesn't work in the latest builds in implicit styles.

    @NMackay
    Thanks for the tip and the confirmation that it doesn't work. Are there any workarounds or is it a known bug?
    I'd love to use SVG but then I'd still need to specify the Width and Height to rasterize the image. Moreover, have an additional Skia dependency and manually manage it.

    Should I file a bug report for this somewhere ?

  • NMackayNMackay mod GBInsider, University mod

    @SurojitXamarin said:

    @NMackay said:
    Just a tip but it's better to have images resources for different screen sizes or svg rather than enforcing a height in a style but yes I've noticed static OnPlatform resources for stuff like font sizes which worked in Forms 2.5 doesn't work in the latest builds in implicit styles.

    @NMackay
    Thanks for the tip and the confirmation that it doesn't work. Are there any workarounds or is it a known bug?
    I'd love to use SVG but then I'd still need to specify the Width and Height to rasterize the image. Moreover, have an additional Skia dependency and manually manage it.

    Should I file a bug report for this somewhere ?

    https://github.com/xamarin/Xamarin.Forms

    File a bug here, I've filed a couple and they've been resolved. Follow the guidelines and attach a repo showing the issue. I did just post another approach, worth checking the backlog as it may have already been raised.

  • SurojitXamarinSurojitXamarin ✭✭ USMember ✭✭

    I tried your method, but even that doesn't work. :(

     <Style TargetType="{x:Type customControls:MyCoolButton}">
            <Setter Property="FontSize">
                <Setter.Value>
                    <OnPlatform x:TypeArguments="x:Double">
                        <On Platform="iOS">10</On>
                        <On Platform="Android,UWP">12</On>
                    </OnPlatform>
                </Setter.Value>
            </Setter>
        </Style>
    
    
  • NMackayNMackay mod GBInsider, University mod

    @SurojitXamarin said:
    I tried your method, but even that doesn't work. :(

     <Style TargetType="{x:Type customControls:MyCoolButton}">
            <Setter Property="FontSize">
                <Setter.Value>
                    <OnPlatform x:TypeArguments="x:Double">
                        <On Platform="iOS">10</On>
                        <On Platform="Android,UWP">12</On>
                    </OnPlatform>
                </Setter.Value>
            </Setter>
        </Style>
    
    

    Well I've worked for 8am til 8:00pm, I'm out of ideas :|

  • SurojitXamarinSurojitXamarin ✭✭ USMember ✭✭

    @NMackay Thanks for all the help. I guess I'll try adding the OnPlatform directly on the elements and get on with it for now.

    Thanks for the help!

  • SurojitXamarinSurojitXamarin ✭✭ USMember ✭✭
    Accepted Answer

    @NMackay
    I tried debugging the issue and it seems WidthRequest is a Double and I was passing it an Int32.
    The method you suggested does work. But not the runtime nor the compiler indicated an error for the type mismatch which led to confusion.

    Thanks!

Sign In or Register to comment.