我正在尝试将两种自定义字体导入我的应用程序。现在,我已经分别在.Droid/Assets
和.iOS/Resources
目录中添加了字体,并且对于 iOS 将它加载到我的 Info.plist 中。
在我的 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>
但是,我希望能够在我的标签中使用多种字体,这就是为什么我将以下代码添加到我的<ResourceDictionary>
:
<Style x:Name="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>
不幸的是,我收到错误:
ResourceDictionary 中已存在键为“Xamarin.Forms.Label”的资源
此外,我是否真的必须使我的 TargetType 特定于标签,如果说,我想全局使用字体,无论它是 aLabel
还是 an Entry
?
首先,对于样式,使用 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>
有时您可以在类型之间共享样式定义。最好的方法是使用 TargetType,它是定义要设置的属性的公共基类。不幸的是,Label 和 Entry 之间没有这种通用的基类。它们各自独立地定义了 FontFamily。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句