Skip to content

Crash when using <FontImageSource /> in Button after upgrading to .NET 9 / Android 35 #30475

Open
@ThiagoFrancischini

Description

@ThiagoFrancischini

Description

I was upgrading my .NET 8 app targeting Android API 34 to .NET 9 targeting Android API 35. During the process, I encountered an issue on a page with the following XAML code:

    <VerticalStackLayout Margin="10">
        <HorizontalStackLayout HorizontalOptions="Fill">
            <Button Command="{Binding OnBackCommand}"
                    Scale="2"
                    Margin="-10,0,0,0"
                    HorizontalOptions="Start"
                    BackgroundColor="Transparent">
                <Button.ImageSource>
                    <FontImageSource FontFamily="MaterialSharp" Color="{DynamicResource MyColor}" Glyph="{x:Static icons:MaterialSharp.Arrow_back}"/>
                </Button.ImageSource>
            </Button>
        </HorizontalStackLayout>
    </VerticalStackLayout>

When I navigate to this page, the app crashes during InitializeComponent() with the following exception:

   at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)
   at System.RuntimeType.CreateInstanceMono(Boolean nonPublic, Boolean wrapExceptions)
   at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean wrapExceptions)
   at System.Activator.CreateInstance(Type type, Boolean nonPublic, Boolean wrapExceptions)
   at System.Activator.CreateInstance(Type type, Boolean nonPublic)
   at System.Activator.CreateInstance(Type type)
   at Microsoft.Maui.Controls.Xaml.CreateValuesVisitor.Visit(ElementNode node, INode parentNode) in /_/src/Controls/src/Xaml/CreateValuesVisitor.cs:line 113
   at Microsoft.Maui.Controls.Xaml.ElementNode.Accept(IXamlNodeVisitor visitor, INode parentNode) in /_/src/Controls/src/Xaml/XamlNode.cs:line 189
   at Microsoft.Maui.Controls.Xaml.ElementNode.Accept(IXamlNodeVisitor visitor, INode parentNode) in /_/src/Controls/src/Xaml/XamlNode.cs:line 183
   at Microsoft.Maui.Controls.Xaml.ElementNode.Accept(IXamlNodeVisitor visitor, INode parentNode) in /_/src/Controls/src/Xaml/XamlNode.cs:line 185
   at Microsoft.Maui.Controls.Xaml.ElementNode.Accept(IXamlNodeVisitor visitor, INode parentNode) in /_/src/Controls/src/Xaml/XamlNode.cs:line 185
   at Microsoft.Maui.Controls.Xaml.RootNode.Accept(IXamlNodeVisitor visitor, INode parentNode) in /_/src/Controls/src/Xaml/XamlNode.cs:line 242
   at Microsoft.Maui.Controls.Xaml.XamlLoader.Visit(RootNode rootnode, HydrationContext visitorContext, Boolean useDesignProperties) in /_/src/Controls/src/Xaml/XamlLoader.cs:line 212
   at Microsoft.Maui.Controls.Xaml.XamlLoader.Load(Object view, String xaml, Assembly rootAssembly, Boolean useDesignProperties) in /_/src/Controls/src/Xaml/XamlLoader.cs:line 82
   at Microsoft.Maui.Controls.Xaml.XamlLoader.Load(Object view, String xaml, Boolean useDesignProperties) in /_/src/Controls/src/Xaml/XamlLoader.cs:line 57
   at Microsoft.Maui.Controls.Xaml.XamlLoader.Load(Object view, Type callingType) in /_/src/Controls/src/Xaml/XamlLoader.cs:line 53
   at Microsoft.Maui.Controls.Xaml.Extensions.LoadFromXaml[PrimeiroAcessoPage](PrimeiroAcessoPage view, Type callingType) in /_/src/Controls/src/Xaml/ViewExtensions.cs:line 42
   at Softbyte.PedidosMobile.MAUI.Views.PrimeiroAcessoPage.InitializeComponent() in C:\Fontes\Softbyte.PedidosMobile\Softbyte.PedidosMobile.MAUI\Softbyte.PedidosMobile.MAUI\obj\Debug\net9.0-android35.0\Microsoft.Maui.Controls.SourceGen\Microsoft.Maui.Controls.SourceGen.CodeBehindGenerator\Views_PrimeiroAcessoPage.xaml.sg.cs:line 23
   at Softbyte.PedidosMobile.MAUI.Views.PrimeiroAcessoPage..ctor() in C:\Fontes\Softbyte.PedidosMobile\Softbyte.PedidosMobile.MAUI\Softbyte.PedidosMobile.MAUI\Views\PrimeiroAcessoPage.xaml.cs:line 11

However, if I simply remove the FontImageSource declaration and leave the button without an image, like bellow, the app works normally:

    <VerticalStackLayout Margin="10">
        <HorizontalStackLayout HorizontalOptions="Fill">
            <Button Command="{Binding OnBackCommand}"
                    Scale="2"
                    Margin="-10,0,0,0"
                    HorizontalOptions="Start"
                    BackgroundColor="Transparent">
            </Button>
        </HorizontalStackLayout>
    </VerticalStackLayout>

I'm confident that all bindings used in this page are valid and correctly implemented, and that any referenced resources or dictionary lookups are present and accessible.

MAUI.Controls version = 9.0.81

Steps to Reproduce

No response

Link to public reproduction project repository

No response

Version with bug

9.0.30 SR3

Is this a regression from previous behavior?

Yes, this used to work in .NET MAUI

Last version that worked well

.NET 8 (Please specify exact version)

Affected platforms

Android

Affected platform versions

Android 15, Android 14, Android 13

Did you find any workaround?

No response

Relevant log output

Metadata

Metadata

Assignees

No one assigned

    Labels

    s/needs-reproAttach a solution or code which reproduces the issuet/bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions