FontFamily已添加,但Xamarin中没有更改

问题描述

我正在尝试在Xamarin应用程序中使用FontFamily。我下载了字体并将其添加到项目中,但仍未对文本应用更改。

Mobile.App 字体文件夹)

catamaran-regular.tff

Mobile.App AssemblyInfo.cs

using Xamarin.Forms;

[assembly: ExportFont("catamaran-regular.ttf")]

Mobile.App App.xml

<Style
    targettype="Label"
    ApplyToDerivedTypes="true">
    <Setter Property="FontFamily" Value="catamaran-regular" />
</Style>

Mobile.App.Views Page.xml

Label
    Text="Hello World!"
    FontSize="18"
    FontFamily="catamaran-regular">
</Label>

解决方法

尝试使用别名来定位字体:

[assembly: ExportFont("catamaran-regular.ttf",Alias = "CatamaranRegular")]

然后在您的元素中使用别名:

<Label
    Text="Hello World!"
    FontSize="18"
    FontFamily="CatamaranRegular">
</Label>

如果仍然无法正常工作,请确保将字体设置为嵌入式资源。如果使用的是Visual Studio,请选择文件,然后从属性面板中的“生成操作”下拉列表中选择选项:

Embedded Resource