[英]C# Custom Fonts not working in Xamarin Forms (Android). Xamarin 4.8. Windows 10, VS19
我遇到了 Xamarin Forms 4.8 自定义 fonts 的问题。
我已经按照这封信的指南进行操作,即:
- 将我的字体文件(.ttf 或.otf)加载到我的共享目录中并将它们标记为嵌入式资源。
- 在我的命名空间之外添加了 ExportFont。
当我在任何地方引用字体时,它只会构建标准字体。 这是一段代码:
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;
}
}
}
我已经尝试将 fonts 放入我的 Android 目录,在 assets 下,但这也不起作用。 作为内置 label 函数的一部分,Xamarin 是否会覆盖我对标准字体所做的一切? 更改文本大小没有问题。
谢谢,
米
我有这个问题。
我的解决方案:更改文件结构
来自:资产/字体/PoppinsBold.ttf
至:Fonts/PoppinsBold.ttf
你可以参考这个官方示例先检查它是否有效。
然后我用你的共享代码进行了测试:
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;
}
它有效:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.