8

Currently I can measure the impressive font icons on my page with tabs from XAML code as follows:

<ContentPage.IconImageSource>
    <FontImageSource  FontFamily="{StaticResource Solid }" Glyph="&#xf108;" ></FontImageSource>
</ContentPage.IconImageSource>

That way I can already put icons in my applications, but I want to do it from the codebehind because that tabbed page will have to be created from certain actions and I did it in the following way:

 contenido.IconImageSource  = new ContentPage().IconImageSource {
    new FontImageSource().FontFamily = "\uf108";
};

but in the first "{" I get the error that says "I expected one ; "

Update

code App.xaml

<Application.Resources>
    <ResourceDictionary>
        <!--Global Styles-->
        <Color x:Key="NavigationPrimary">#2196F3</Color>
        <Style TargetType="NavigationPage">
            <Setter Property="BarBackgroundColor" Value="{StaticResource NavigationPrimary}" />
            <Setter Property="BarTextColor" Value="White" />
        </Style>

        <OnPlatform x:TypeArguments="x:String" 
            x:Key="Brands">
            <On Platform="Android" 
      Value="BrandsRegular.otf#Regular" />
        </OnPlatform>

        <OnPlatform x:TypeArguments="x:String" 
            x:Key="Regular">
            <On Platform="Android" 
      Value="FreeRegular.otf#Regular" />
        </OnPlatform>

        <OnPlatform x:TypeArguments="x:String" 
            x:Key="Solid">
            <On Platform="Android" 
      Value="FreeSolid.otf#Regular" />
        </OnPlatform>


    </ResourceDictionary>
</Application.Resources>

1 Answer 1

9

You don't need to create a new ContentPage, just create a FontImageSource object and set the values accordingly then pass this object to the IconImageSource of your contenido Page.

Also, you will need to get the StaticResource from the App Resources Dictionary.

Something like this should work:

var solidFontFamily = Application.Current.Resources["Solid"] as OnPlatform<string>;
contenido.IconImageSource = new FontImageSource() { FontFamily = solidFontFamily, Glyph= "\uf108" };

Hope this helps.-

6
  • this line "var solidFontFamily = Application.Current.Resources["Solid"] as string;" return null
    – David
    Feb 15, 2020 at 3:33
  • How do you have defined the StaticResurce “Solid” in your App.xml Resources?
    – pinedax
    Feb 15, 2020 at 3:46
  • I Updated the post with the way have defined the StaticResurce “Solid”.
    – David
    Feb 15, 2020 at 4:42
  • 3
    The code is right but you cast is wrong, you have to cast OnPlatform<string> instead of string, like this var solidFontFamily = Application.Current.Resources["Solid"] as OnPlatform<string>; Feb 15, 2020 at 5:21
  • @MuhammadAdeelShoukat you're right. Thanks for the comment. I've just updated the code and it should work now.
    – pinedax
    Feb 15, 2020 at 18:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.