Xamarin.Forms: 如何在 C# 代码中使用自定义字体

Xamarin.Forms: How to use custom Fonts in C# Code

我想在我的 Xamarin.Forms 应用程序中使用一些自定义字体 (.ttf)。 我在两个项目中添加了两种字体(Android/iOS):

现在在 XAML-Page 中,我将字体添加到 ResourceDictionary。 也可以使用字体,但只能在 XAML-File:

<Label Text="Test" FontFamily="{StaticResource FONT}" FontSize="Medium"/>

但是如何在 C# 代码中使用这种字体?

Device.RuntimePlatform属性可以在各个平台设置不同的字体名称

if (Device.RuntimePlatform == Device.iOS)
{
    label.FontFamily = "xxx.ttf";
}

else if (Device.RuntimePlatform == Device.Android)
{
    label.FontFamily = "xxx.ttf#xxx";
}

else
{
    label.FontFamily = "Assets/Fonts/xxx.ttf#xxx";
}