使用 c# 更改 FontAwesome 文本和字体系列 - xamarin.forms [英] Change FontAwesome text and fontfamily with c# - xamarin.forms
问题描述
我在我的解决方案(android 和 ios)中添加了 FontAwesome 字体,我可以毫无问题地从 xaml 使用它.
I added the FontAwesome fonts in my solutions (android and ios) and i can use it from xaml without any problem.
我的 xaml 代码
<Label x:Name="Star1" Text="" FontSize="22" FontFamily="{StaticResource FontAwesomeRegular}"/>
应用程序.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);
Application.Current.Resources["FontAwesomeSolid"].ToString() 返回给我
The Application.Current.Resources["FontAwesomeSolid"].ToString() returns me
"Xamarin.Forms.OnPlatform`1[System.String]"
"Xamarin.Forms.OnPlatform`1[System.String]"
推荐答案
问题在于OnPlatform
的使用.我不确定为什么它不会像这样工作,我想这与它在内部的铸造方式有关.
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.
试试这个,应该可以:
Star1.FontFamily = (Xamarin.Forms.OnPlatform<string>)Application.Current.Resources["FontAwesomeSolid"];
这篇关于使用 c# 更改 FontAwesome 文本和字体系列 - xamarin.forms的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!