【问题标题】:How to use an icon from an embedded resource font file in Xamarin Forms如何在 Xamarin Forms 中使用嵌入资源字体文件中的图标
【发布时间】:2020-06-19 20:01:46
【问题描述】:

我想在我的 Xamarin Forms 应用程序中使用 Font Awesome 中的图标。我已经按照我找到的几个教程进行操作,但我仍然无法显示图标。这是显示类名和命名空间的静态类的一部分:

namespace NGIC_XAML.Constants
{
    public static class IconFontsFAProRegular
    {
        public const string GlassMartini = "\uf000";
        public const string Music = "\uf001";
        public const string Search = "\uf002";
        public const string Heart = "\uf004";
    }
}

这是我的 AssemblyInfo.cs 文件中的 ExportFont 语句:

[assembly: ExportFont("Font-Awesome-5-Pro-Regular-400.otf", Alias = "FAProRegular")]

这是我的 XAML 文件中命名空间的声明:

             xmlns:local="clr-namespace:NGIC_XAML.Constants"

最后,这是我想使用其中一个字体图标的标签:

                    <Image
                        HeightRequest="44"
                        HorizontalOptions="Center"
                        VerticalOptions="Center"
                        WidthRequest="44">
                        <Image.Source>
                            <FontImageSource
                                FontFamily="FAProRegular"
                                Glyph="{x:Static local:IconFontsFAProRegular.Heart}"
                                Size="44"
                                Color="{StaticResource NGIC_Red}"  />
                        </Image.Source>
                    </Image>

输出

肯定有人会问,所以这里是安装的包:

我很想知道我做错了什么!当我在 ImageSource 中输入命名空间名称时,我也没有得到智能感知。

【问题讨论】:

标签: xamarin xamarin.forms icons font-awesome-5 embedded-fonts


【解决方案1】:

您必须将字体系列设置为静态资源并在代码中引用它。否则将无法正常工作。

                   <Image.Source>
                        <FontImageSource
                            FontFamily="{StaticResource FontAwesomeRegular}"
                            Glyph="{x:Static local:IconFontsFAProRegular.Heart}"
                            Size="44"
                            Color="{StaticResource NGIC_Red}"  />

例子:

<OnPlatform x:Key="FontAwesomeBrands" x:TypeArguments="x:String">
    <On Platform="Android" Value="FontAwesome5Brands.otf#Regular" />
    <On Platform="iOS" Value="FontAwesome5Brands-Regular" />
    <On Platform="UWP" Value="/Assets/FontAwesome5Brands.otf#Font Awesome 5 Brands" />
</OnPlatform>

<OnPlatform x:Key="FontAwesomeSolid" x:TypeArguments="x:String">
    <On Platform="Android" Value="FontAwesome5Solid.otf#Regular" />
    <On Platform="iOS" Value="FontAwesome5Free-Solid" />
    <On Platform="UWP" Value="/Assets/FontAwesome5Solid.otf#Font Awesome 5 Free" />
</OnPlatform>

<OnPlatform x:Key="FontAwesomeRegular" x:TypeArguments="x:String">
    <On Platform="Android" Value="FontAwesome5Regular.otf#Regular" />
    <On Platform="iOS" Value="FontAwesome5Free-Regular" />
    <On Platform="UWP" Value="/Assets/FontAwesome5Regular.otf#Font Awesome 5 Free" />
</OnPlatform>

【讨论】:

    【解决方案2】:

    在 XAML 文件中使用 C# 代码时,添加程序集很重要,这样您就可以毫无问题地使用字体图标。

    xmlns:local="clr-namespace:NGIC_XAML.Constants;assembly=NGIC_XAML"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-26
      • 2019-11-08
      • 2019-03-18
      • 1970-01-01
      • 2018-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多