问题描述
嗨,我当前的 xamarin 应用程序没有全局 App.xaml 文件,只有一个 App.cs,这是我配置它的方式,并希望保持这种方式。
现在这个问题,我想使用 fontawsome 中的一些字体图标,但要使其工作,我需要定义一个可以在 xaml 文件中使用的资源字典。现在我正在将资源复制粘贴到我需要图标的每个 xaml 页面。
我希望资源在全局范围内工作,所以有没有办法将我的代码转换为 C# 或在我的全局 App.xaml 文件中实现资源,这样我就不必一直复制/粘贴它。
这是我用于我的资源的代码:
<ResourceDictionary>
<OnPlatform x:TypeArguments="x:String"
x:Key="FontAwesomeBrands">
<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:TypeArguments="x:String"
x:Key="FontAwesomeSolid">
<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:TypeArguments="x:String"
x:Key="FontAwesomeRegular">
<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>
</ResourceDictionary>
解决方法
Xamarin 更新了您引用字体的方式,而且比以前容易多了。
现在,您所要做的就是:
- 在您的 iOS 和 Android 项目作为参考添加的程序集中添加您的字体
- 将构建操作(文件属性)设置为嵌入式资源
- 更新已添加字体的程序集的 AssemblyInfo
- 在FontFamily中设置字体的别名
- 你很高兴使用这些字体
编辑: 如果您无法升级到最新的 XF,请按以下步骤操作(请抓紧,它更复杂):
- iOS:
-
在您的 iOS 项目中,在 Resources 文件夹下,将字体添加为 BundleResource
-
使用此代码编辑您的 Info.plist
<!-- Custom fonts --> <key>UIAppFonts</key> <array> <string>Fonts/FontAwesome-Regular.otf</string> <string>Fonts/FontAwesome-Solid.otf</string> <string>Fonts/FontAwesome-Light.otf</string> </array>
- 安卓:
- 将字体添加为 AndroidAsset(文件属性)
- Xamarin 表单:
-
在您的 App.xaml 资源中创建针对这些字体的资源
<!--#region FONTS--> <OnPlatform x:Key="FAR" x:TypeArguments="x:String"> <On Platform="Android" Value="Fonts/FontAwesome-Regular.otf#FontAwesome5ProRegular" /> <On Platform="iOS" Value="FontAwesome5Pro-Regular" /> <On Platform="UWP" Value="Fonts/FontAwesome-Regular.otf#Font Awesome 5 Pro" /> <On Platform="Default" Value="Fonts/FontAwesome-Regular.otf" /> </OnPlatform> <OnPlatform x:Key="FAS" x:TypeArguments="x:String"> <On Platform="Android" Value="Fonts/FontAwesome-Solid.otf#FontAwesome5ProSolid" /> <On Platform="iOS" Value="FontAwesome5Pro-Solid" /> <On Platform="UWP" Value="Fonts/FontAwesome-Solid.otf#Font Awesome 5 Pro" /> <On Platform="Default" Value="Fonts/FontAwesome-Solid.otf" /> </OnPlatform> <OnPlatform x:Key="FAL" x:TypeArguments="x:String"> <On Platform="Android" Value="Fonts/FontAwesome-Light.otf#FontAwesome5ProLight" /> <On Platform="iOS" Value="FontAwesome5Pro-Light" /> <On Platform="UWP" Value="Fonts/FontAwesome-Light.otf#Font Awesome 5 Pro" /> <On Platform="Default" Value="Fonts/FontAwesome-Light.otf" /> </OnPlatform> <!--#endregion FONTS-->
-
使用带有 StaticResource 的字体(但不带有别名)(删除代码中和给定代码中 Label 之前的空格:编辑器正在吃它们,所以我不得不添加一个空格以使其可见)
请注意:很难找到要使用的字体路径名:在我给出的第一个示例中,Xamarin 正在为您完成这项工作。用老办法,得自己找名字(第一次找各个平台的名字有点麻烦)。您可以通过在计算机上安装字体名称或在文件属性的详细信息中找到字体名称。
/!\ 也许它适用于字体名称中的空格。我不知道为什么它对我有空格不起作用。因此可能不需要删除字体名称中的空格。
您可以在 Xamarin Forms App.xaml 部分的代码隐藏中执行相同的操作。
希望能帮到你