Xamarin.Forms更改所有应用程序中的默认FontFamily

问题描述

添加自定义字体:

[assembly: ExportFont("Roboto-Regular.ttf",Alias ="Roboto")]

也在App.xaml中:

        <Style targettype="Label">
            <Setter Property="FontFamily" Value="Roboto"/>
        </Style>

还可以添加Entry等。 但是是否可以用一行代码更改在创建项目时初始化的认字体系列?如果可能怎么办?

解决方法

您需要为标签,条目,选择器等创建控件。使用默认字体系列。我为您创建了一个标签控件

public class MyFontLabel : Label
{
    public MyFontLabel()
    {
       Style = Application.Current.Resources["FontLabelStyle"] as Style;
    }
}
app.xaml中的

样式

<Style x:Key="FontLabelStyle" TargetType="Label">
  <Setter Property="FontFamily" Value="Roboto"/>
</Style>

在整个过程中使用它

<local:MyFontLabel Text="My Text"/>