简体   繁体   English

C# 自定义 Fonts 在 Xamarin Forms (Android) 中不起作用。 Xamarin 4.8。 Windows 10, VS19

[英]C# Custom Fonts not working in Xamarin Forms (Android). Xamarin 4.8. Windows 10, VS19

I'm having an issue with Xamarin Forms 4.8 custom fonts.我遇到了 Xamarin Forms 4.8 自定义 fonts 的问题。

I've followed the guides to the letter, that is:我已经按照这封信的指南进行操作,即:

-Loaded my font files (.ttf or.otf) into my shared directory and marked them as Embedded resources. - 将我的字体文件(.ttf 或.otf)加载到我的共享目录中并将它们标记为嵌入式资源。

-Added the ExportFont outside of my namespace. - 在我的命名空间之外添加了 ExportFont。

When I reference the font anywhere it just builds the standard font.当我在任何地方引用字体时,它只会构建标准字体。 Here is a snippet of code:这是一段代码:

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;
}
}
}

I've experimented placing the fonts into my Android directory, under assets but that doesn't work either.我已经尝试将 fonts 放入我的 Android 目录,在 assets 下,但这也不起作用。 Is Xamarin overwriting whatever I do with standard Font as part of the built in label functions?作为内置 label 函数的一部分,Xamarin 是否会覆盖我对标准字体所做的一切? I have no problems changing the text size.更改文本大小没有问题。

Thanks,谢谢,

M

I had this issue.我有这个问题。

My solution: Changing file structure我的解决方案:更改文件结构

FROM: Assets/Fonts/PoppinsBold.ttf来自:资产/字体/PoppinsBold.ttf

To: Fonts/PoppinsBold.ttf至:Fonts/PoppinsBold.ttf

You could refer to this official sample to check whether it works first.你可以参考这个官方示例先检查它是否有效。

Then I tested with your shared code:然后我用你的共享代码进行了测试:

    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;
    }

And it works:它有效:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM