我怎样才能在 XAML/C# 中显示超棒的字体字形
How can I display a font awesome glyph in XAML/C#
我在Visual Studio中尝试了很多东西,但我仍然无法显示字形。
- 我需要添加 NuGet 包吗?哪个?
- 我需要 C# 中的 'using ...;' 吗?
- 我需要 XAML 中的 'xmlns:...' 吗?
- 我应该用 "Label" 显示它吗?
我知道在 HTML 中,您只需在元素中添加一个 class。但我在 XAML 中难住了。
(它是跨平台的,对吧?)
这应该是一件简单的事情,但我无法得到任何功能的答案。如果你能做到这一点,请提供答案并回答所有要点。
Xamarin.Forms 有一个正确的 plugin。这个插件不仅有FontAwesome字体。
根据 this example in 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
class,OnCreate()方法添加:
FormsPlugin.Iconize.Droid.IconControls.Init(Resource.Id.toolbar);
Plugin.Iconize.Iconize.With(new Plugin.Iconize.Fonts.FontAwesomeModule());
在iOS项目中,AppDelegate
class,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.0.0.29-beta\lib\UAP10\
中创建一个名为 Plugin.Iconize.FontAwesome.UWP
的文件夹
其次,在 Plugin.Iconize.FontAwesome.UWP
文件夹中创建另一个名为 Assets
的文件夹
第三次在 Assets
文件夹
中创建另一个名为 Fonts
的文件夹
最后复制 iconize-fontawesome.ttf 文件(你不应该更改他们的名字)。
Additionally, you have to add a Fonts folder in the Assets folder in your UWP project and then paste the same ttf file
This is not necessary on Android or iOS it is only a issue of the UWP platform
尝试 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)
我在Visual Studio中尝试了很多东西,但我仍然无法显示字形。
- 我需要添加 NuGet 包吗?哪个?
- 我需要 C# 中的 'using ...;' 吗?
- 我需要 XAML 中的 'xmlns:...' 吗?
- 我应该用 "Label" 显示它吗?
我知道在 HTML 中,您只需在元素中添加一个 class。但我在 XAML 中难住了。 (它是跨平台的,对吧?)
这应该是一件简单的事情,但我无法得到任何功能的答案。如果你能做到这一点,请提供答案并回答所有要点。
Xamarin.Forms 有一个正确的 plugin。这个插件不仅有FontAwesome字体。
根据 this example in 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
class,OnCreate()方法添加:
FormsPlugin.Iconize.Droid.IconControls.Init(Resource.Id.toolbar);
Plugin.Iconize.Iconize.With(new Plugin.Iconize.Fonts.FontAwesomeModule());
在iOS项目中,AppDelegate
class,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.0.0.29-beta\lib\UAP10\
Plugin.Iconize.FontAwesome.UWP
的文件夹
其次,在 Plugin.Iconize.FontAwesome.UWP
文件夹中创建另一个名为 Assets
的文件夹
第三次在 Assets
文件夹
Fonts
的文件夹
最后复制 iconize-fontawesome.ttf 文件(你不应该更改他们的名字)。
Additionally, you have to add a Fonts folder in the Assets folder in your UWP project and then paste the same ttf file
This is not necessary on Android or iOS it is only a issue of the UWP platform
尝试 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)