如何在StaticResource中设置字体?

时间:2019-08-13 14:11:55

标签: xaml xamarin.forms

我正在尝试在App.xaml中定义FontAwesome fontFamily,以便可以在代码中使用staticresource。

直接在页面中设置fontFamily起作用。问题是,当我尝试从StaticResource获取此值时,显示空白页。

App.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="&#xf007;" 
                       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>

Icon类的内容:

  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";
...
}

我在这里做什么错了?

1 个答案:

答案 0 :(得分:1)

简化字体文件名并使用字体名称,如下所示。

Android似乎对字体名称并不严格,但是,如果字体文件名不正确,则会引发RuntimeException:“找不到字体资产”。

在iOS上,如果UIAppFonts的{​​{1}}数组键中的字体文件名不正确,或者字体名称不正确,则字形/文本会在框架中显示为问号。

在UWP上,如果字体文件名或字体系列名称不正确,字形/文本将显示为框架。

项目中的字体文件位置和内容类型:

iOS:字体文件位置:Info.plist文件夹。内容类型:Resources

Android:字体文件位置:BundleResource文件夹。内容类型:Assets

UWP:字体文件位置:AndroidAsset文件夹。内容类型: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

对于iOS,请在<Label Text="&#xf2b9;" TextColor="Accent" FontFamily="{StaticResource FaRegular}" /> <Label Text="&#xf641;" TextColor="Accent" FontFamily="{StaticResource FaSolid}" /> <Label Text="&#xf26e;" TextColor="Accent" FontFamily="{StaticResource FaBrands}" /> 中使用“打开方式”> XML编辑器添加以下内容:

Info.plist