【问题标题】:How can I display a font awesome glyph in XAML/C#如何在 XAML/C# 中显示字体真棒字形
【发布时间】:2018-02-14 06:41:32
【问题描述】:

我在 Visual Studio 中尝试了很多东西,但仍然无法显示字形。

  1. 是否需要添加 NuGet 包?哪个?
  2. 我是否需要“使用 ...;”在 C# 中?
  3. 我需要 XAML 中的“xmlns:...”吗?
  4. 我应该用“标签”显示它吗?

我知道在 HTML 中,您只需向元素添加一个类。但我对 XAML 感到困惑。 (它是跨平台的,对吧?)

这应该是一件简单的事情,但我无法得到任何函数的答案。如果您能做到这一点并回答所有问题,请提供答案。

【问题讨论】:

  • @Flot2011 我似乎无法正常工作。每当我有“FontAwsome ...”时,仍然会得到一个红色下划线。秘诀是什么?
  • @Jason 这也是 UWP 的形式吗?
  • @Flot2011link 适用于 WinForms,而非 Xamarin Forms。它们是完全不同的东西。如果您想知道 Iconize 支持哪些平台,请尝试阅读文档
  • 使用这个 url 获取你想要的 unicode 作为 c# 类:github.com/fzany/Font-Awesome-Cheat-Charp

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


【解决方案1】:

对于 Xamarin.Forms,有一个正确的 plugin。这个插件不仅有FontAwesome字体。

根据this example in github,您必须执行以下操作:

<StackLayout Orientation="Horizontal">
        <iconize:IconLabel FontSize="20" Text="{Binding Key}" TextColor="Green" VerticalTextAlignment="Center" />
</StackLayout>

[更新]

应用户 3247130 的要求,我留下完整的示例:

从 Visual Studio 或 Xamarin Studio,安装以下包:

  • Xam.Plugin.Iconize
  • Xam.Plugin.Iconize.FontSwesome
  • Xam.FormsPlugin.Iconize

在Android项目中,MainActivity类,OnCreate()方法添加:

FormsPlugin.Iconize.Droid.IconControls.Init(Resource.Id.toolbar);
Plugin.Iconize.Iconize.With(new Plugin.Iconize.Fonts.FontAwesomeModule());

在iOS项目中,AppDelegate类,FinishedLaunching()方法,添加类似行:

FormsPlugin.Iconize.iOS.IconControls.Init();
Plugin.Iconize.Iconize.With(new Plugin.Iconize.Fonts.FontAwesomeModule());

另外,在 iOS 项目中,添加 info.plist

<key>UIAppFonts</key>
<array>     
    <string>iconize-fontawesome.ttf</string>
</array>

现在,在您有工具栏的XAML 中,在标签中添加

<ContentPage ...
xmlns:iconize="clr-namespace:FormsPlugin.Iconize;assembly=FormsPlugin.Iconize">

<iconize:IconLabel Text="fa-search"  />

[更新 2]

此插件在 UWP 平台上存在问题。 要在 UWP 平台上运行此插件,请按照以下步骤操作:

首先,在%userprofile%\.nuget\packages\xam.plugin.iconize.fontawesome‌​\2.0.0.29-beta\lib\U‌​AP10\中创建一个名为Plugin.Iconize.FontAwesome.UWP的文件夹

其次,在Plugin.Iconize.FontAwesome.UWP文件夹中创建另一个名为Assets的文件夹

第三次创建另一个名为 Fonts 的文件夹到 Assets 文件夹中

最后复制iconize-fontawesome.ttf文件(你不应该改变他们的名字)。

另外,你必须在你的 UWP 项目的 Assets 文件夹中添加一个 Fonts 文件夹,然后粘贴相同的 ttf 文件

这在 Android 或 iOS 上不是必需的,它只是 UWP 平台的问题

【讨论】:

  • 您可以创建一个gist 以查看您的源代码吗?
  • 您要部署到哪个平台?
  • 哦对了!!这是一个 UWP 平台问题。要解决它,您必须将文件 tff (download here) 复制到以下路径:%userprofile%\.nuget\packages\xam.plugin.iconize.fontawesome\2.0.0.29-beta\lib\UAP10\Plugin。 Iconize.FontAwesome.UWP\Assets\Fonts
  • 您很可能没有 Plugin.Iconize.FontAwesome.UWP\Assets\Fonts 文件夹,因此您应该转到 %userprofile%\。 nuget\packages\xam.plugin.iconize.fontawesome\2.0.0.29 -beta\lib\UAP10 并创建这些文件夹,然后您可以复制 ttf 文件
  • 这在Android或iOS上不是必需的,它只是UWP平台的问题。
【解决方案2】:

试试Plugin.Glypher,它支持 Font Awesome 5 Free/Pro 和 WeatherIcons。

xmlns:fontAwesome5Free="clr-namespace:Plugin.Glypher.FontAwesome5Free;assembly=Plugin.Glypher.FontAwesome5Free"
xmlns:glypher="clr-namespace:Plugin.Glypher;assembly=Plugin.Glypher"

<Label glypher:FontGlyph.Glyph="{x:Static fontAwesome5Free:GlyphList.Fab_Bluetooth}"
       FontSize="Large"
       TextColor="CornflowerBlue" />

<Button glypher:FontGlyph.Glyph="{x:Static fontAwesome5Free:GlyphList.Fab_Bitcoin}"
        FontSize="Large"
        TextColor="IndianRed" />

<Image>
    <Image.Source>
        <FontImageSource Size="32" 
                         Color="Orange" 
                         glypher:FontGlyph.Glyph="{x:Static fontAwesome5Free:GlyphList.Far_Bell_Slash}" />
    </Image.Source>
</Image>

这一切都在共享代码中。但这取决于 Xamarin.Forms (>= 3.6.0.344457)

【讨论】:

    猜你喜欢
    • 2021-09-20
    • 1970-01-01
    • 1970-01-01
    • 2013-12-15
    • 2020-04-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-12
    • 1970-01-01
    相关资源
    最近更新 更多