dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
21.98k stars 1.71k forks source link

[WINDOWS, 9.0 Preview 6] AppShell.Navigated throws System.ArgumentException in Microsoft.Maui.FontManager #23678

Open janne-hmp opened 1 month ago

janne-hmp commented 1 month ago

Description

We see in Windows app the following handled exception: System.ArgumentException: The parameter is incorrect. Assets/Fonts/Lato.ttf is not a valid absolute URI. It is thrown by WinRTUriRuntimeClassFactory.CreateUriForMarshaling(string uri), and logged by Microsoft.Maui.FontManager. I do not have a Fonts directory under Assets (but rather under Resources).

It happens in AppShell.Navigated that has been called by AppShell.Appearing.

That font seems to be some sort of a default font. I tried adding it to Resources\Fonts, but it did not seem to help with the exception.

Steps to Reproduce

  1. Start GnollHackM on Windows.
  2. SentrySDK logs the exception.

Link to public reproduction project repository

https://github.com/hyvanmielenpelit/GnollHack

Version with bug

9.0.0-preview.6.24327.7

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

Windows

Affected platform versions

Windows 11

Did you find any workaround?

The exception is handled, so this only seems to produce unnecessary logging. It might cause some problems elsewhere potentially.

Relevant log output

exception
System.ArgumentException: The parameter is incorrect.

Assets/Fonts/Lato.otf is not a valid absolute URI.
Error
13:57:57
Microsoft.Maui.FontManager
Error loading font 'Assets/Fonts/Lato.ttf'.

{
exception_message: 
The parameter is incorrect.

Assets/Fonts/Lato.ttf is not a valid absolute URI.
}
Error
13:57:57
navigation
AppShell.Navigated

{
from: ,
Source: ShellItemChanged,
to: //MainPage
}
Info
13:57:57
navigation
App.PageAppearing

{
Page: MainPage
}
Info
13:57:57
navigation
App.PageAppearing

{
Page: AppShell
}
Info
13:57:56
ui.lifecycle
AppShell.Appearing
Info
13:57:56
github-actions[bot] commented 1 month ago

Hi I'm an AI powered bot that finds similar issues based off the issue title.

Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one and thumbs upping the other issue to help us prioritize it. Thank you!

Open similar issues:

Closed similar issues:

Note: You can give me feedback by thumbs upping or thumbs downing this comment.

janne-hmp commented 1 month ago

The problem seems to be that Assets/Fonts/Lato.ttf is not an absolute URI, not so much that the file does not exist.

PureWeen commented 1 month ago

@janne-hmp are you not seeing this issue on net8?

janne-hmp commented 1 month ago

@janne-hmp are you not seeing this issue on net8?

Not sure, as our app is built using .NET 9. SkiaSharp did not work properly in .NET 8 MAUI (at least for our app). .NET 9 in turn worked well.

QianaJiao commented 4 weeks ago

I can repro this issue at Windows platform on the latest main 17.12.0 Preview 2.0 (MAUI Version: 9.0.0-preview.6.24327.7 & 9.0.0-preview.5.24307.10). image

beeradmoore commented 3 weeks ago

I am also getting this issue, but I am on net8 with MAUI 8.0.80. Same code runs fine on macOS.

System.ArgumentException: The parameter is incorrect.

Assets/Fonts/ProximaNovaSemiBold.otf is not a valid absolute URI.
   at WinRT.ExceptionHelpers.<ThrowExceptionForHR>g__Throw|39_0(Int32 hr)
   at WinRT.ExceptionHelpers.ThrowExceptionForHR(Int32 hr)
   at ABI.System.WinRTUriRuntimeClassFactory.CreateUriForMarshaling(String uri)
   at ABI.System.Uri.CreateMarshaler2(Uri value)
   at Microsoft.Graphics.Canvas.Text.CanvasFontSet._ICanvasFontSetFactory.Create(Uri uri)
   at Microsoft.Graphics.Canvas.Text.CanvasFontSet..ctor(Uri uri)
   at Microsoft.Maui.FontManager.FindFontFamilyName(String fontFile)

EDIT 1: Oh.. something is weird here. I am not using ProximaNovaSemiBold.otf.

image

            .ConfigureFonts(fonts =>
            {
                fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
                fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
                fonts.AddFont("proxima_nova_bold.ttf", "ProximaNovaBold");
                fonts.AddFont("proxima_nova_regular.ttf", "ProximaNovaRegular");
                fonts.AddFont("proxima_nova_semibold.ttf", "ProximaNovaSemibold");
            })

EDIT 2: Well, my problem is "fixed". Apparelty I was naming the font ProximaNovaRegular in MauiProgram.cs, but I was using it as ProximaNova-Regular in styles. I couldn't see the correct combination of what these should be. So I just changed everything to this format,

fonts.AddFont("proxima_nova_regular.ttf", "proxima_nova_regular");

and it seems to go away. Looks like something is not finding the font and falling back to disk.

EDIT 3: Just to confirm I updated my style to look like this,

<Style TargetType="Label">
    <Setter Property="FontFamily" Value="test_font" />
</Style>

and then when I run my app I see these errors.

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

System.ArgumentException: The parameter is incorrect.

Assets/Fonts/test_font.ttf is not a valid absolute URI.
   at WinRT.ExceptionHelpers.<ThrowExceptionForHR>g__Throw|39_0(Int32 hr)
   at WinRT.ExceptionHelpers.ThrowExceptionForHR(Int32 hr)
   at ABI.System.WinRTUriRuntimeClassFactory.CreateUriForMarshaling(String uri)
   at ABI.System.Uri.CreateMarshaler2(Uri value)
   at Microsoft.Graphics.Canvas.Text.CanvasFontSet._ICanvasFontSetFactory.Create(Uri uri)
   at Microsoft.Graphics.Canvas.Text.CanvasFontSet..ctor(Uri uri)
   at Microsoft.Maui.FontManager.FindFontFamilyName(String fontFile)
Exception thrown: 'System.ArgumentException' in WinRT.Runtime.dll
WinRT information: Assets/Fonts/test_font.otf is not a valid absolute URI.
Microsoft.Maui.FontManager: Error: Error loading font 'Assets/Fonts/test_font.otf'.

System.ArgumentException: The parameter is incorrect.

Assets/Fonts/test_font.otf is not a valid absolute URI.
   at WinRT.ExceptionHelpers.<ThrowExceptionForHR>g__Throw|39_0(Int32 hr)
   at WinRT.ExceptionHelpers.ThrowExceptionForHR(Int32 hr)
   at ABI.System.WinRTUriRuntimeClassFactory.CreateUriForMarshaling(String uri)
   at ABI.System.Uri.CreateMarshaler2(Uri value)
   at Microsoft.Graphics.Canvas.Text.CanvasFontSet._ICanvasFontSetFactory.Create(Uri uri)
   at Microsoft.Graphics.Canvas.Text.CanvasFontSet..ctor(Uri uri)
   at Microsoft.Maui.FontManager.FindFontFamilyName(String fontFile)