AathifMahir / MauiIcons

MauiIcons is Icon Collection Library for .Net Maui
MIT License
196 stars 13 forks source link

Warning: Unable to load font 'MaterialIcons'. (and other fonts) #94

Closed ChrisK91 closed 4 months ago

ChrisK91 commented 4 months ago

Describe the bug I'm trying to use the package on a new MAUI app. Using the package even a completely blank MAUI.

Several errors are logged, the first one being

Microsoft.Maui.FontRegistrar: Warning: Unable to load font 'MaterialIcons'.

System.InvalidOperationException: Operation is not valid due to the current state of the object.
   at WinRT.ExceptionHelpers.<ThrowExceptionForHR>g__Throw|39_0(Int32 hr)
   at WinRT.ExceptionHelpers.ThrowExceptionForHR(Int32 hr)
   at ABI.Windows.Storage.IApplicationDataStaticsMethods.get_Current(IObjectReference _obj)
   at Windows.Storage.ApplicationData.get_Current()
   at Microsoft.Maui.EmbeddedFontLoader.LoadFont(EmbeddedFont font)
   at Microsoft.Maui.FontRegistrar.LoadEmbeddedFont(String cacheKey, String filename, String alias, Stream stream)
   at Microsoft.Maui.FontRegistrar.GetFont(String font)

When trying to render the page, errors regarding the missing font are logged:

Microsoft.Maui.FontManager: Error: Error loading font 'Assets/Fonts/MaterialIcons.ttf'.

System.InvalidOperationException: This operation is not supported for a relative URI.
   at System.Uri.get_AbsolutePath()
   at Microsoft.Maui.FontManager.FindFontFamilyName(String fontFile)
Microsoft.Maui.FontManager: Error: Error loading font 'Assets/Fonts/MaterialIcons.otf'.

System.InvalidOperationException: This operation is not supported for a relative URI.
   at System.Uri.get_AbsolutePath()
   at Microsoft.Maui.FontManager.FindFontFamilyName(String fontFile)

To Reproduce

  1. Add <PackageReference Include="AathifMahir.Maui.MauiIcons.Material" Version="2.1.5" />, which then looks like this:

    <ItemGroup>
        <PackageReference Include="Microsoft.Maui.Controls" Version="$(MauiVersion)" />
        <PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="$(MauiVersion)" />
        <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="8.0.0" />
        <PackageReference Include="AathifMahir.Maui.MauiIcons.Material" Version="2.1.5" />
    </ItemGroup>
  1. Change MauiProgram.cs
        builder
            .UseMauiApp<App>()
            .UseMaterialMauiIcons()
            .ConfigureFonts(fonts =>
            {
                fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
                fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
            });
  1. Add Icon & change code-behind to prevent XAML error
using MauiIcons.Core;

namespace Test;

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();
        _ = new MauiIcon();

    }
}
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:mi="http://www.aathifmahir.com/dotnet/2022/maui/icons"
             x:Class="Test.MainPage">

    <ScrollView>
        <StackLayout>
            <mi:MauiIcon Icon="{mi:Material ABC}"/>
        </StackLayout>
    </ScrollView>

</ContentPage>
  1. Compile and run application

Expected behavior Icon should load

Screenshots If applicable, add screenshots to help explain your problem.

Desktop and Smartphone (please complete the following information):

Additional context I've tried adding the ttf into the resource folder as well, without any success

AathifMahir commented 4 months ago

@ChrisK91 Can you try adding UseMauiMaterialIcon Extension after Configure Fonts like below example? I assume Configure Fonts Method Causing this issue

builder.UseMauiApp<App>()
            .ConfigureFonts(fonts =>
            {
                fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
                fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
            })
.UseMaterialMauiIcons();
ChrisK91 commented 4 months ago

Thanks for your fast reply @AathifMahir ! I changed the order, without any success. I also tried loading/including the font myself, without any success. It seems like quite an obscure error, an internet search has not yet resulted in any solutions for me.

AathifMahir commented 4 months ago

Thanks for your fast reply @AathifMahir ! I changed the order, without any success. I also tried loading/including the font myself, without any success. It seems like quite an obscure error, an internet search has not yet resulted in any solutions for me.

I'll try my best to reproduce this issue as soon as possible. Additionally can you try to reproduce this issue on Android as well using Emulator or Real Device.

ChrisK91 commented 4 months ago

I'll look into that. Currently my local debugging for android is not working, because my user directory contains a space... I'll see if I can work around that.

Edit: I've got it to work on Android, where the icon is displayed without issues. So it seems to be related to Windows.

AathifMahir commented 4 months ago

I'll look into that. Currently my local debugging for android is not working, because my user directory contains a space... I'll see if I can work around that.

Edit: I've got it to work on Android, where the icon is displayed without issues. So it seems to be related to Windows.

The problem here, I can't reproduce this on Windows or Android or any other platforms? As far I can see from the error logs you have submitted. I assume it has to do with Maui SDK, I think something went wrong with the installation or completely something else that we are yet to diagnose

AathifMahir commented 4 months ago

@ChrisK91 I have closed this issue since there's no activity on this,if your still facing the same issue, feel free comment on this issue for further discussion or reopening

ChrisK91 commented 4 months ago

The problem still exists, however I'm currently working around it by embedding the relevant icons as SVGs directly. It might be related to dotnet or maui itself. I'll see if one of the framework updates fixes it in the future. Thanks for your help!

AathifMahir commented 4 months ago

The problem still exists, however I'm currently working around it by embedding the relevant icons as SVGs directly. It might be related to dotnet or maui itself. I'll see if one of the framework updates fixes it in the future. Thanks for your help!

I'm not sure, if it is related to Maui Itself. I can't seem to reproduce this issue at all with various different Maui Version.

Did you tried reinstalling Maui Workload?

ChrisK91 commented 4 months ago

I just did a complete reinstallation of the dotnet sdk and maui, without any changes. I will report back, if anything changes with a future update.

litaniast commented 4 months ago

I have same issue using new version 2.2.5. When I downgraded 2.1.5, works fine. I'm using MAUI CommunityToolkit (latest version).

AathifMahir commented 4 months ago

I have same issue using new version 2.2.5. When I downgraded 2.1.5, works fine. I'm using MAUI CommunityToolkit (latest version).

The problem here is, I can't seem to reproduce this issue whatsoever, I have been trying different Maui Version and etc.. still everything works 🤔

ChrisK91 commented 4 months ago

Are you running Windows 10? Maybe it's an issue with the Windows version.

AathifMahir commented 4 months ago

Are you running Windows 10? Maybe it's an issue with the Windows version.

I'm on Windows 11