Xamarin.forms 无特定TargetType的应用程序的多个自定义字体

Xamarin.forms 无特定TargetType的应用程序的多个自定义字体,xamarin.forms,Xamarin.forms,我正在尝试将两种自定义字体导入到我的应用程序中。现在,我已经分别在.Droid/Assets和.iOS/Resources目录中添加了字体,并在我的Info.plist中加载了字体 在my App.xaml中,我执行以下操作以加载PCL中的字体: <Application.Resources> <ResourceDictionary> <Style x:Name="BoldFont" TargetType="Label">

我正在尝试将两种自定义字体导入到我的应用程序中。现在,我已经分别在
.Droid/Assets
.iOS/Resources
目录中添加了字体,并在我的Info.plist中加载了字体

在my App.xaml中,我执行以下操作以加载PCL中的字体:

<Application.Resources>
    <ResourceDictionary>
        <Style x:Name="BoldFont" TargetType="Label">
            <Setter Property="Label.FontFamily">
                <Setter.Value>
                    <OnPlatform x:TypeArguments="x:String">
                        <OnPlatform.Android>PT_Sans-Narrow-Web-Regular.ttf#PT Sans Narrow</OnPlatform.Android>
                        <OnPlatform.iOS>PT Sans Narrow</OnPlatform.iOS>
                    </OnPlatform>
                </Setter.Value>
            </Setter>
        </Style>
    </ResourceDictionary>
</Application.Resources>

PT#u Sans-shown-Web-Regular.ttf#PT Sans-shown
PT Sans窄带
但是,我希望能够在标签中使用多种字体,这就是为什么我在
中添加了以下代码的原因:


Roboto压缩正则。ttf#Roboto压缩
机器人浓缩
不幸的是,我得到了一个错误:

ResourceDictionary中已存在键为“Xamarin.Forms.Label”的资源


此外,如果我想全局使用字体,而不管它是
标签
还是
条目

首先,对于样式,使用x:Key而不是x:Name。这将避免您看到的错误消息

<Style x:Key="RegularFont" TargetType="Label">
    <Setter Property="Label.FontFamily">
        <Setter.Value>
            <OnPlatform x:TypeArguments="x:String">
                <OnPlatform.Android>RobotoCondensed-Regular.ttf#Roboto Condensed</OnPlatform.Android>
                <OnPlatform.iOS>Roboto Condensed</OnPlatform.iOS>
            </OnPlatform>
        </Setter.Value>
    </Setter>
</Style>

Roboto压缩正则。ttf#Roboto压缩
机器人浓缩
有时,可以在类型之间共享样式定义。最好的方法是使用TargetType,它是定义要设置的属性的公共基类。不幸的是,标签和条目之间没有这样的公共基类。它们各自独立地定义了FontFamily

<Style x:Key="RegularFont" TargetType="Label">
    <Setter Property="Label.FontFamily">
        <Setter.Value>
            <OnPlatform x:TypeArguments="x:String">
                <OnPlatform.Android>RobotoCondensed-Regular.ttf#Roboto Condensed</OnPlatform.Android>
                <OnPlatform.iOS>Roboto Condensed</OnPlatform.iOS>
            </OnPlatform>
        </Setter.Value>
    </Setter>
</Style>