.NET Maui - Can you download fonts from server side? instead of storing within the project? - Stack Overflow

i am aware of how to use fonts the standard way within the MauiProgram.csBut i am trying to see if its

i am aware of how to use fonts the standard way within the MauiProgram.cs

But i am trying to see if its possible to download a font from server side and then bind it programmatically, but to no success as of yet. Here is some code i have attempted to implement.

public async Task<string> DownloadAndSetFontAsync(string fontName, string fontTechnicalName)
 {
     var fontPath = Path.Combine(FileSystem.CacheDirectory, $"{fontTechnicalName}.ttf");
     using var client = new HttpClient();
     var fontData = await client.GetByteArrayAsync("https://xxxxxx/fonts/"+ fontTechnicalName +".ttf");
     await File.WriteAllBytesAsync(fontPath, fontData);
     return GetFontName(fontPath);
 }

The code above successfully downloads the font to a users cache but does not render on the front end, even when the property is raised? any thoughts?

i am aware of how to use fonts the standard way within the MauiProgram.cs

But i am trying to see if its possible to download a font from server side and then bind it programmatically, but to no success as of yet. Here is some code i have attempted to implement.

public async Task<string> DownloadAndSetFontAsync(string fontName, string fontTechnicalName)
 {
     var fontPath = Path.Combine(FileSystem.CacheDirectory, $"{fontTechnicalName}.ttf");
     using var client = new HttpClient();
     var fontData = await client.GetByteArrayAsync("https://xxxxxx/fonts/"+ fontTechnicalName +".ttf");
     await File.WriteAllBytesAsync(fontPath, fontData);
     return GetFontName(fontPath);
 }

The code above successfully downloads the font to a users cache but does not render on the front end, even when the property is raised? any thoughts?

Share Improve this question asked Nov 20, 2024 at 19:46 Dean BeckertonDean Beckerton 3073 silver badges10 bronze badges 3
  • The .ttf file should be put in the Resources\Fonts folder of the project, where its build action will automatically be set to MauiFont. If you put it in the other folder, you must manually set it to MauiFont in the Properties window – Guangyu Bai - MSFT Commented Nov 21, 2024 at 2:49
  • Please read the question I know how to do that I am more curious on how to do it server side – Dean Beckerton Commented Nov 21, 2024 at 7:32
  • @DeanBeckerton If you do not like my answer, I will delete it. ( I know it is not really an answer) – H.A.H. Commented Nov 21, 2024 at 12:14
Add a comment  | 

1 Answer 1

Reset to default 0

Example:

public MainPage(IFontRegistrar fontRegistrar)
{
    ...
    if(useMaterial3)
        fontRegistrar.Register("MaterialIcons-3.ttf", "Icons");
    else 
        fontRegistrar.Register("MaterialIcons-2.ttf", "Icons");
    ...
}

<Label Text="{x:Static local:MaterialIconsFont.Engineering}" FontFamily="Icons" FontSize="40"/>

This works for me.

It registers different font at runtime. Runtime of internal (ad-hoc) app that not a single soul will miss, if one day it stops working.

I would not write something like that for production app, even if my life depended on it.

Additional disclaimer: I did read your question, and I understand the "download" part. I want to point out the registration part and how it gets resolved when the XAML is loaded.

I know it doesn't really answer your question, but you may find it useful.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742333600a4424207.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信