如何在项目中使用Font Awesome图标作为ImageButton的图标
How to use Font Awesome icons in project as an icon of ImageButton
我在了解如何在我的 Xamarin 应用程序中使用 Font Awesome 图标时遇到问题,我想将它与 ImageButton
作为图标一起使用。我发现的大多数教程都没有帮助我理解它是如何工作的。
如 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
属性的静态 class,每个属性都具有与图标字形对应的代码作为值,作为名称,您需要提供对它的描述只有描述而不是代码,就像我对 Glyph="{x:Static fonts:IconFont.AddressBook}"
所做的那样,它看起来像这样:
static class IconFont
{
public const string Ad = "\uf641";
public const string AddressBook = "\uf2b9";
...
}
我邀请您关注此 video tutorial and check out this GitHub Page,它从您提交的字体字形文件开始为您生成静态 c# class。
我在了解如何在我的 Xamarin 应用程序中使用 Font Awesome 图标时遇到问题,我想将它与 ImageButton
作为图标一起使用。我发现的大多数教程都没有帮助我理解它是如何工作的。
如 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
属性的静态 class,每个属性都具有与图标字形对应的代码作为值,作为名称,您需要提供对它的描述只有描述而不是代码,就像我对 Glyph="{x:Static fonts:IconFont.AddressBook}"
所做的那样,它看起来像这样:
static class IconFont
{
public const string Ad = "\uf641";
public const string AddressBook = "\uf2b9";
...
}
我邀请您关注此 video tutorial and check out this GitHub Page,它从您提交的字体字形文件开始为您生成静态 c# class。