admin管理员组文章数量:1332339
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.
本文标签: NET MauiCan you download fonts from server side instead of storing within the projectStack Overflow
版权声明:本文标题:.NET Maui - Can you download fonts from server side? instead of storing within the project? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742333599a2455182.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论