簡體   English   中英

如何在XAML / C#中顯示超棒的字形

[英]How can I display a font awesome glyph in XAML/C#

我已經在Visual Studio中嘗試了很多東西,但是仍然無法顯示字形。

  1. 我需要添加一個NuGet包嗎? 哪一個?
  2. 我需要一個“使用...”嗎? 在C#中?
  3. 我需要在XAML中使用“ xmlns:...”嗎?
  4. 我應該用“標簽”顯示它嗎?

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

這應該是一件簡單的事情,但是我無法獲得功能的任何答案。 如果可以的話,請提供答案並回答所有要點。

Xamarin.Forms有正確的插件 該插件不僅具有FontAwesome字體。

根據github中的此示例,您必須執行以下操作:

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

[更新]

應user3247130的要求,我留下完整的示例:

從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的文件夾

其次,創建另一個名為Assets into Plugin.Iconize.FontAwesome.UWP文件夾的文件夾

第三個創建另一個文件夾,稱為“將Fonts放入Assets文件夾

最后復制iconize-fontawesome.ttf文件(您不應更改其名稱)。

此外,您必須在UWP項目的Assets文件夾中添加Fonts文件夾,然后粘貼相同的ttf文件

在Android或iOS上這不是必需的,這只是UWP平台的問題

試試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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM