簡體   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

我遇到了 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM