如Microsoft Documentation中所述:
- 您需要首先拥有我认为
.ttf(或.otf)的字体文件。
- 将其添加到您的共享项目中。
- 右键单击该文件并单击“属性”,然后将其构建操作设置为Embedded Resource。
- 在您的
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>
更新
更好地使用ImageButton 和FontImageSource 属性而不是标签,您还具有按钮的单击功能,我还发现有趣的是您可以更改字形图标的颜色,天气硬编码或动态取决于在selected theme,这里有一个例子:
<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";
...
}
我邀请您关注此video tutorial 并查看this GitHub Page,它会从您提交的字体字形文件开始为您生成静态 c# 类。