Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

put awesome font icons in c # code behind

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>
like image 448
David Avatar asked Feb 15 '20 01:02

David


People also ask

Can I upload icon to Font Awesome?

Now with the magic of Kits, you can upload your own icons and use them right alongside official Font Awesome ones!


1 Answers

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.-

like image 181
pinedax Avatar answered Oct 03 '22 16:10

pinedax