【问题标题】:C# Custom Fonts not working in Xamarin Forms (Android). Xamarin 4.8. Windows 10, VS19C# 自定义字体在 Xamarin 表单 (Android) 中不起作用。 Xamarin 4.8。视窗 10、VS19
【发布时间】:2020-10-09 21:59:14
【问题描述】:

我在使用 Xamarin Forms 4.8 自定义字体时遇到问题。

我已按照信中的指南进行操作,即:

-将我的字体文件(.ttf 或 .otf)加载到我的共享目录中并将它们标记为嵌入式资源。

-在我的命名空间之外添加了 ExportFont。

当我在任何地方引用该字体时,它只会构建标准字体。这是一段sn-p代码:

using System;
using System.Collections.Generic;
using Xamarin.Forms;
using Newtonsoft.Json.Linq;
using System.IO;
using System.Reflection;

[assembly: ExportFont("Stencil.ttf")]
[assembly: ExportFont("GreatVibes.otf")]
[assembly: ExportFont("Lobster-Regular.ttf", Alias= "MyFont")]

namespace BAAaM
{
    public partial class MainPage : ContentPage
    {

        public MainPage()
        {

            Grid topgrid = new Grid();
            topgrid.Children.Add(new Label { Text = "Test, please work", FontSize = 30, FontFamily = "Lobster-Regular" }, 0, 1);
this.Content = topgrid;
}
}
}

我已经尝试将字体放入我的 Android 目录中的 assets 下,但这也不起作用。作为内置标签功能的一部分,Xamarin 是否会覆盖我使用标准字体所做的任何事情?更改文本大小没有问题。

谢谢,

M

【问题讨论】:

    标签: c# android xamarin fonts


    【解决方案1】:

    你可以先参考this official sample看看是否有效。

    然后我用你的共享代码进行了测试:

        public FontPageXaml ()
        {
            InitializeComponent ();
            Grid topgrid = new Grid();
            topgrid.Children.Add(new Label { Text = "Test, please work", FontSize = 30, FontFamily = "Lobster-Regular" }, 0, 1);
            this.Content = topgrid;
        }
    

    它的工作原理:

    【讨论】:

      【解决方案2】:

      我遇到了这个问题。

      我的解决方案:更改文件结构

      来自: 资产/字体/PoppinsBold.ttf

      到: 字体/PoppinsBold.ttf

      【讨论】:

        猜你喜欢
        • 2018-02-15
        • 1970-01-01
        • 1970-01-01
        • 2018-09-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多