问题描述
观看 James Montemagno video guide on fonts in Xamarin 后,我在选项卡视图或按钮中显示图标时遇到问题。 Intellisense 可以工作,它可以识别从 Mathew's github 下载的 FontAwesomeIcons.cs 文件中的所有字体,但将它们显示为带有正方形 的 X。找不到任何可行的解决方案。一切都是最新的。
assemblyinfo.cs:
using Xamarin.Forms.Xaml;
[assembly: XamlCompilation(XamlCompilationoptions.Compile)]
[assembly: ExportFont("Font Awesome 5 Brands-Regular-400",Alias = "FAB")]
[assembly: ExportFont("Font Awesome 5 Free-Regular-400",Alias = "FAR")]
[assembly: ExportFont("Font Awesome 5 Free-Solid-900",Alias = "FAS")]
应用程序外壳:
<Shell.Resources>
<ResourceDictionary>
<Style targettype="ShellContent" x:Key="PricingPage">
<Setter Property="Icon">
<Setter.Value>
<FontimageSource FontFamily="FAS" Glyph="{x:Static fontAwesome:FontAwesomeIcons.BalanceScale}"/>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
</Shell.Resources>
<TabBar>
<Tab Title="PricingPage" Style="{StaticResource PricingPage}" >
<ShellContent Title="PricingPage" Route="PricingPage" ContentTemplate="{DataTemplate local:ProductPricing}" />
<ShellContent Title="ExistingProductPricing" IsVisible="False" Route="ExistingProductPricing" Shell.FlyoutBehavior="disabled" ContentTemplate="{DataTemplate local:ExistingProductPricing}" />
</Tab>
</TabBar>
使用命名空间工作的页面中的按钮:
<Button
Text="{x:Static fontAwesome:FontAwesomeIcons.CheckCircle}"
FontFamily="FAS"/>
在安卓模拟器中可以看到 X,在我的手机上,如果重要的话,它只是一个空白方块。
如果还有什么需要我再补充。提前感谢所有帮助
解决方法
我认为缺少的是您应该在 ExportFont
中包含文件扩展名:
using Xamarin.Forms.Xaml;
[assembly: XamlCompilation(XamlCompilationOptions.Compile)]
[assembly: ExportFont("Font Awesome 5 Brands-Regular-400.ttf",Alias = "FAB")]
[assembly: ExportFont("Font Awesome 5 Free-Regular-400.ttf",Alias = "FAR")]
[assembly: ExportFont("Font Awesome 5 Free-Solid-900.tff",Alias = "FAS")]
更多详情How to use Font Awesome icons in project as an icon of ImageButton