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
1 Answer
Reset to default 0Example:
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条)