xaml - 如何在 StaticResource 中设置字体?
问题描述
我正在尝试在 App.xaml 中定义 FontAwesome fontFamily,以便可以在我的代码中使用 staticresource。
直接在页面中设置 fontFamily 是有效的。问题是当我尝试从 StaticResource 获取此值时,我显示了一个空白页面。
应用程序.xaml:
<?xml version="1.0" encoding="utf-8" ?>
<prism:PrismApplication xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:prism="clr-namespace:Prism.Unity;assembly=Prism.Unity.Forms"
x:Class="RedPrairie.App">
<Application.Resources>
<!-- Application resource dictionary -->
<ResourceDictionary>
<!-- Font Awesome fonts path per plateform -->
<OnPlatform x:Key="FontAwesomeBrands" x:TypeArguments="x:String" Android="FontAwesome5Brands.otf#Font Awesome 5 Brands Regular" iOS="Font Awesome 5 Free" />
<OnPlatform x:Key="FontAwesomeSolid" x:TypeArguments="x:String" Android="FontAwesome5Solid.otf#Font Awesome 5 Free Solid" iOS="Font Awesome 5 Free" />
<OnPlatform x:Key="FontAwesomeRegular" x:TypeArguments="x:String" Android="FontAwesome5Regular.otf#Font Awesome 5 Free Regular" iOS="Font Awesome 5 Free" />
</ResourceDictionary>
</Application.Resources>
</prism:PrismApplication>
我的页面:
<Label Text="{x:Static model:Icon.far_user}"
FontFamily="{StaticResource FontAwesomeRegular}"
FontSize="Large"
TextColor="SkyBlue"
VerticalOptions="Center">
</Label>
工作页面:
<Label Text="{x:Static model:Icon.far_user}"
FontSize="Large"
TextColor="SkyBlue"
VerticalOptions="Center">
<Label.FontFamily>
<OnPlatform
x:TypeArguments="x:String"
Android="FontAwesome5Regular.otf#Font Awesome 5 Free Regular"
iOS="Font Awesome 5 Free" />
</Label.FontFamily>
</Label>
另一个工作页面:
<Label Text=""
FontSize="Large"
TextColor="SkyBlue"
VerticalOptions="Center">
<Label.FontFamily>
<OnPlatform
x:TypeArguments="x:String"
Android="FontAwesome5Regular.otf#Font Awesome 5 Free Regular"
iOS="Font Awesome 5 Free" />
</Label.FontFamily>
</Label>
类图标的内容:
public static class Icon
{
/*Regular Icons - prefixed with FAR_*/
public static string far_address_book = "\uf2b9";
public static string far_address_card = "\uf2bb";
public static string far_angry = "\uf556";
public static string far_arrow_alt_circle_down = "\uf358";
public static string far_arrow_alt_circle_left = "\uf359";
public static string far_arrow_alt_circle_right = "\uf35a";
public static string far_arrow_alt_circle_up = "\uf35b";
public static string far_user = "\uf007";
...
}
我在这里做错了什么?
解决方案
简化字体文件名并使用如下所示的字体名称。
似乎 Android 对字体名称并不严格,但是,如果字体文件名不正确,则会抛出 RuntimeException: 'Font asset not found '。
在 iOS 上,如果UIAppFonts
数组键中的字体文件名不正确Info.plist
,或者字体名称不正确,则字形/文本在框架内显示为问号。
在 UWP 上,如果字体文件名或字体系列名称不正确,字形/文本仅显示为框架。
项目中的字体文件位置和内容类型:
iOS:字体文件位置:Resources
文件夹。内容类型:BundleResource
Android:字体文件位置:Assets
文件夹。内容类型:AndroidAsset
UWP:字体文件位置:Assets
文件夹。内容类型:Content
ResourceDictionary
:
<OnPlatform x:TypeArguments="x:String" x:Key="FaRegular">
<On Platform="iOS" Value="FontAwesome5Free-Regular" />
<On Platform="Android" Value="FontAwesome5Regular.otf#Font Awesome Regular" />
<On Platform="UWP" Value="Assets/FontAwesome5Regular.otf#Font Awesome 5 Free" />
</OnPlatform>
<OnPlatform x:TypeArguments="x:String" x:Key="FaSolid">
<On Platform="iOS" Value="FontAwesome5Free-Solid" />
<On Platform="Android" Value="FontAwesome5Solid.otf#Font Awesome Solid" />
<On Platform="UWP" Value="Assets/FontAwesome5Solid.otf#Font Awesome 5 Free" />
</OnPlatform>
<OnPlatform x:TypeArguments="x:String" x:Key="FaBrands">
<On Platform="iOS" Value="FontAwesome5Brands-Regular" />
<On Platform="Android" Value="FontAwesome5Brands.otf#Font Awesome Brands" />
<On Platform="UWP" Value="Assets/FontAwesome5 Brands.otf#Font Awesome 5 Brands" />
</OnPlatform>
示例Label
:
<Label Text="" TextColor="Accent" FontFamily="{StaticResource FaRegular}" />
<Label Text="" TextColor="Accent" FontFamily="{StaticResource FaSolid}" />
<Label Text="" TextColor="Accent" FontFamily="{StaticResource FaBrands}" />
对于 iOS,使用 Open With > XML 编辑器添加以下内容Info.plist
:
<key>UIAppFonts</key>
<array>
<string>FontAwesome5Brands.otf</string>
<string>FontAwesome5Regular.otf</string>
<string>FontAwesome5Solid.otf</string>
</array>
推荐阅读
- python - 数据框中的Python for循环
- java - Java - 有条件地显示空值 xml 标记
- c# - 尝试在 C# 中生成按钮
- python - 为什么 str.contains() 没有返回正确的结果?
- r - 在R中将全名与反向匹配
- magento2 - 我在哪里可以找到所有可以在 pages/blocks/cms 内容中使用的 {{ codes }}?
- python - Python循环数据框并在IF中添加2个条件以达到最终索引并增加它返回错误
- pid - 启动后无法打开 PID 文件 /var/run/teamviewerd.pid(还没有?):没有这样的文件或目录
- html - 单击后使用 ngfor 保持活动列表项
- go - ssh 会话打开请求最终在 ssh 连接上给出 EOF