我在使用Font Awesome图标在Xamarin应用程序中时遇到了问题,我想将其作为ImageButton
的图标使用。但大多数教程都没有帮助我理解它的工作原理。
我在使用Font Awesome图标在Xamarin应用程序中时遇到了问题,我想将其作为ImageButton
的图标使用。但大多数教程都没有帮助我理解它的工作原理。
.ttf
(或 .otf) 格式。AssemblyInfo.cs
或 App.xaml.cs
中使用友好的别名名称导出它:[assembly: ExportFont("file-name.ttf", Alias = "FontAwesome")]
<Label FontFamily="FontAwesome" Text=""/>
如果您需要图标的代码列表,请查看FontAwesome codes。
如果您想要像按钮一样具有点击功能,则可以使用带有GestureRecognizers的标签:
<Label FontFamily="FontAwesome" Text="">
<Label.GestureRecognizers>
<TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped"/>
</Label.GestureRecognizers>
</Label>
最好使用带有FontImageSource
属性的ImageButton
代替标签,这样您就可以像按钮一样具有单击功能。我发现有趣的是,您可以更改字形图标的颜色,无论是硬编码还是动态地根据所选主题,以下是示例:
<ImageButton>
<ImageButton.Source>
<FontImageSource FontFamily="FontAwesome"
Glyph="{x:Static fonts:IconFont.AddressBook}"
Color="{AppThemeBinding Dark=White,
Light=Black}"/>
</ImageButton.Source>
</ImageButton>
您还可以定义一个静态类,其中包含const string
属性,每个属性的值都是对应图标字形的代码,名称是其描述,这样您只需要提供描述而不是代码,就像我使用Glyph="{x:Static fonts:IconFont.AddressBook}"
一样,它看起来会像这样:
static class IconFont
{
public const string Ad = "\uf641";
public const string AddressBook = "\uf2b9";
...
}
我邀请您跟随这个视频教程,并查看这个 GitHub 页面,它可以为您生成一个静态的 C# 类,该类是从您提交的字体图标文件开始生成的。
xmlns:constants="clr-namespace:YOUR_APP_NAMESPACE"
然后,我必须像这样写出Glyph属性:
Glyph="{x:Static Member=constants:Constants.AddressBook}"
- gadildafisshFontAwesome 支持多个框架(Vue、React、Angular、WordPress、LESS、SCSS),但我不知道为什么他们没有为 Xamarin Forms 和 MAUI 提供支持。
我已经为 Xamarin Forms 和 MAUI 创建了自定义控件。
有关说明,请访问https://www.nuget.org/packages/Brushtail.FontAwesome.Mobile/。
欢迎反馈意见。