c# - 使用 c# 更改 FontAwesome 文本和 fontfamily - xamarin.forms
问题描述
我在我的解决方案(android 和 ios)中添加了 FontAwesome 字体,我可以毫无问题地从 xaml 使用它。
我的xml代码
<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>
我想在点击时更改某些标签中的 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]”
解决方案
问题是使用OnPlatform
. 我不确定为什么它不会像这样工作,我想这与它在内部的铸造方式有关。
试试这个,它应该工作:
Star1.FontFamily = (Xamarin.Forms.OnPlatform<string>)Application.Current.Resources["FontAwesomeSolid"];
推荐阅读
- reactjs - 反应原生(expo)加载降价文件
- loops - 在乳胶中,如何循环遍历为每个元素创建关联或宏的数组?
- gstreamer - GStreamer 音频管道在 Linux 上工作,但在 Windows 上不工作
- java - 上传时如何在 Dropbox 中将文件名设置为 FileMetadata
- c - C在变量范围改变时给出奇怪的输出
- c# - UWP 不支持的视频类型 - 要捕获哪个事件?
- java - 使用正则表达式从字符串中提取数字
- vb.net - 在 C# 中使用原始打印进行阿拉伯语文本打印
- c - 为什么我在 c 中使用堆栈对后缀代码的中缀不能正常工作?
- c# - 将文本框值从 Windows 窗体传递给类