简体   繁体   中英

Change FontAwesome text and fontfamily with c# - xamarin.forms

I added the FontAwesome fonts in my solutions (android and ios) and i can use it from xaml without any problem.

My xaml code

   <Label x:Name="Star1" Text="&#xf005;" FontSize="22" FontFamily="{StaticResource FontAwesomeRegular}"/>

App.xaml

        <OnPlatform x:TypeArguments="x:String" 
                    x:Key="FontAwesomeSolid"> 
          <On Platform="Android" 
              Value="FontAwesome5Solid.otf#Regular" /> 
          <On Platform="iOS" 
              Value="FontAwesome5Free-Solid" />  
        </OnPlatform> 

        <OnPlatform x:TypeArguments="x:String" 
                    x:Key="FontAwesomeRegular">
          <On Platform="Android" 
              Value="FontAwesome5Regular.otf#Regular" /> 
          <On Platform="iOS" 
              Value="FontAwesome5Free-Regular" />
        </OnPlatform>

I want to change the fontfamily and Text value in some label on tap. I try the following code but is not working.

        TapGestureRecognizer starEvent = new TapGestureRecognizer();
        starEvent.Tapped += (sender, e) => {
            Star1.FontFamily = Application.Current.Resources["FontAwesomeSolid"].ToString();
            Star1.Text = "\uf005";
        };

        Star1.GestureRecognizers.Add(starEvent);

The Application.Current.Resources["FontAwesomeSolid"].ToString() returns me

"Xamarin.Forms.OnPlatform`1[System.String]"

The problem is the use of OnPlatform . I'm not sure why it won't work like this, I guess it's related to how it's casted internally.

Try this, it should work:

Star1.FontFamily = (Xamarin.Forms.OnPlatform<string>)Application.Current.Resources["FontAwesomeSolid"];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM