【问题标题】:Change FontAwesome text and fontfamily with c# - xamarin.forms使用 c# 更改 FontAwesome 文本和 fontfamily - xamarin.forms
【发布时间】:2020-06-05 09:38:40
【问题描述】:

我在我的解决方案(android 和 ios)中添加了 FontAwesome 字体,我可以在 xaml 中毫无问题地使用它。

我的 xaml 代码

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

我想在点击时更改某些标签中的 fontfamily 和 Text 值。我尝试了以下代码,但没有工作。

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

        Star1.GestureRecognizers.Add(starEvent);

Application.Current.Resources["FontAwesomeSolid"].ToString() 返回我

“Xamarin.Forms.OnPlatform`1[System.String]”

【问题讨论】:

    标签: c# xamarin xamarin.forms


    【解决方案1】:

    问题在于OnPlatform的使用。我不确定为什么它不会像这样工作,我想这与它在内部的铸造方式有关。

    试试这个,它应该可以工作:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多