【问题标题】:How to use Font Awesome icons in project as an icon of ImageButton如何在项目中使用 Font Awesome 图标作为 ImageButton 的图标
【发布时间】:2021-03-13 15:33:11
【问题描述】:

我在实现如何在我的 Xamarin 应用程序中使用 Font Awesome 图标时遇到问题,我想将它与 ImageButtonas 图标一起使用。而且我发现的大多数教程都无法帮助我理解它是如何工作的。

【问题讨论】:

  • 您遇到了什么具体问题?
  • 如何实现和使用字体真棒图标

标签: c# xaml xamarin xamarin.forms font-awesome


【解决方案1】:

Microsoft Documentation中所述:

  1. 您需要首先拥有我认为.ttf(或.otf)的字体文件。
  2. 将其添加到您的共享项目中。
  3. 右键单击该文件并单击“属性”,然后将其构建操作设置为Embedded Resource
  4. 在您的AssemblyInfo.csApp.xaml.cs 中使用友好别名导出它:

[assembly: ExportFont("file-name.ttf", Alias = "FontAwesome")]

  1. 消费它:
<Label FontFamily="FontAwesome" Text="&#xf004;"/>

有关图标代码列表,请查看FontAwesome codes


如果您想将其与按钮等点击功能一起使用,则可以使用带有GestureRecognizers 的标签:

<Label FontFamily="FontAwesome" Text="&#xf004;">
     <Label.GestureRecognizers>
         <TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped"/>
     </Label.GestureRecognizers>
</Label>

更新

更好地使用ImageButtonFontImageSource 属性而不是标签,您还具有按钮的单击功能,我还发现有趣的是您可以更改字形图标的颜色,天气硬编码或动态取决于在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# 类。

【讨论】:

  • 如果我想将它用作 ImageButton 中的图标怎么办?
  • 任何其他使用方式,例如在标签或 etec 中。看起来像按钮(可点击)
  • 是的,用 GestureRecognizers 检查我的更新
  • 很高兴知道:)
猜你喜欢
  • 2013-08-11
  • 1970-01-01
  • 2016-11-13
  • 2022-08-08
  • 2014-01-13
  • 2017-03-16
  • 2013-01-22
  • 2013-05-07
  • 1970-01-01
相关资源
最近更新 更多