Troubleshooting Missing Fonts in Unpackaged Deployment
Environment
Product | Version |
---|---|
Progress® Telerik® UI for .NET MAUI | 6.5.0 |
Description
I am experiencing an issue with missing fonts in an unpackaged deployment of my .NET MAUI app. When I publish the app as an exe, the fonts are not loaded. I have followed the documentation and tried different approaches, but the issue persists.
Solution
To resolve the issue of missing fonts in an unpackaged deployment of a .NET MAUI app, follow these steps:
- Add the font files used in your app to the
Resources/Fonts
folder of your project. - Right-click on each font file and set the Build Action property to Copy Always.
- Update the
MauiProgram.cs
file and register the font files by adding the following code:
.ConfigureFonts(fonts =>
{
fonts.AddFont("font1.ttf", "Font1");
fonts.AddFont("font2.ttf", "Font2");
// Add more font files if necessary
});
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.UseTelerik()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
fonts.AddFont("telerikfontexamples.ttf", "telerikfontexamples");
fonts.AddFont("telerikcontrolsicons.ttf", "telerikfontexamples");
});
#if WINDOWS10_0_17763_0_OR_GREATER
Microsoft.Maui.Handlers.LabelHandler.Mapper.AppendToMapping("FontFamily", (handler, element) =>
{
/* An unpackaged app does not have ms-appx://Assets/ folder available. To avoid issues you can explicitly define the new path to the font file
*
* - Packaged => "ms-appx://Assets/Fonts/Font.ttf#familyname"
* - Unpackaged => "font.ttf#familyname"
*
* In order for the unpackaged approach to work, be sure to include the font's file with the project's assets and set the Build Action to 'CopyAlways'
*/
handler.PlatformView.FontFamily = element.Font.Family.ToLower() switch
{
"telerikcontrolsicons" => new Microsoft.UI.Xaml.Media.FontFamily("telerikcontrolsicons.ttf#telerikcontrolsicons"),
"telerikfontexamples" => new Microsoft.UI.Xaml.Media.FontFamily("telerikfontexamples.ttf#telerikfontexamples"),
_ => handler.PlatformView.FontFamily
};
});
#endif
return builder.Build();
}
Note: If you are looking for the font files used in the previous code example, you can find them in the Telerik UI for Maui Samples source code. This prpblem is not specific to Telerik fonts, it can happen with any font that is not available by default.
Further Assistance
If the issue still persists after following these steps, please provide a reproducible project and reach out to the Microsoft MAUI team for further investigation. You can get assistance in two locations:
- .NET MAUI on GitHub (always search open Issues before creating a new Issue
- Microsoft Q&A for .NET MAUI
Notes
- Make sure to use the correct spelling and case sensitivity when registering the font files in the
MauiProgram.cs
file. - Test the app on both Windows 10 and Windows Server 2019 to ensure compatibility.
- Stay updated with the latest versions of Telerik UI for .NET MAUI and the Microsoft MAUI framework to benefit from any bug fixes or improvements.
- Refer to the official Microsoft MAUI documentation for more information on working with custom fonts in .NET MAUI.