Repository: CommunityToolkit/Maui

Issue #2415: [BUG] AppThemeResourceExtension crashes the app after updating the community toolkit version to 10.0.0
Created: 2024-12-26T19:05:26Z
Is there an existing issue for this?

 I have searched the existing issues

Did you read the "Reporting a bug" section on Contributing file?

 I have read the "Reporting a bug" section on Contributing file: https://github.com/CommunityToolkit/Maui/blob/main/CONTRIBUTING.md#reporting-a-bug

Current Behavior
AppThemeResourceExtension crashes the app during initialization on all platforms in Release mode. In Debug, it works as expected.
If the line with AppThemeResource is removed, no exception will be thrown.
<ContentPage
    x:Class="MauiApp1.MainPage"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
    BackgroundColor="{toolkit:AppThemeResource PageBackgroundColor}">
<toolkit:AppThemeColor
    x:Key="PageBackgroundColor"
    Dark="#000000"
    Light="#f0f0f6" />
Android Emulator exception
FATAL EXCEPTION: main
Process: com.companyname.mauiapp1, PID: 5745
android.runtime.JavaProxyThrowable: [System.ArgumentException]: Arg_ArgumentException Arg_ParamName_Name, serviceProvider
at CommunityToolkit.Maui.Extensions.AppThemeResourceExtension.ProvideValue + 0x39(Unknown Source)
at MauiApp1.MainPage.InitializeComponent + 0xca(Unknown Source)
at MauiApp1.MainPage..ctor + 0x6(Unknown Source)
at Unknown.Unknown(Unknown Source)
at System.Reflection.ConstructorInvoker.InterpretedInvoke + 0x0(Unknown Source)
at System.Reflection.ConstructorInvoker.InvokeDirectByRefWithFewArgs + 0x7d(Unknown Source)
at System.Reflection.ConstructorInvoker.InvokeDirectByRef + 0x29(Unknown Source)
at System.Reflection.ConstructorInvoker.InvokeImpl + 0xbe(Unknown Source)
at System.Reflection.ConstructorInvoker.Invoke + 0x47(Unknown Source)
at Microsoft.Extensions.DependencyInjection.ActivatorUtilities+ConstructorMatcher.CreateInstance + 0xc8(Unknown Source)
at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.CreateInstance + 0x1d7(Unknown Source)
at Microsoft.Maui.Controls.ShellContent+<>c__DisplayClass20_0.<Microsoft.Maui.Controls.IShellContentController.GetOrCreateContent>b__0 + 0x68(Unknown Source)
at Microsoft.Maui.Controls.ElementTemplate.CreateContent + 0x27(Unknown Source)
at Microsoft.Maui.Controls.Internals.DataTemplateExtensions.CreateContent + 0x8(Unknown Source)
at Microsoft.Maui.Controls.ShellContent.Microsoft.Maui.Controls.IShellContentController.GetOrCreateContent + 0x69(Unknown Source)
at Microsoft.Maui.Controls.Platform.Compatibility.ShellSectionRenderer.OnCreateView + 0x16e(Unknown Source)
at AndroidX.Fragment.App.Fragment.n_OnCreateView_Landroid_view_LayoutInflater_Landroid_view_ViewGroup_Landroid_os_Bundle_ + 0x20(Unknown Source)
at Unknown.Unknown(Unknown Source)
at Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPLLL_L + 0x5(Unknown Source)
at crc640ec207abc449b2ca.ShellSectionRenderer.n_onCreateView(Native Method)
at crc640ec207abc449b2ca.ShellSectionRenderer.onCreateView(ShellSectionRenderer.java:41)
at androidx.fragment.app.Fragment.performCreateView(Fragment.java:3119)
at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:577)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:286)
at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2164)
at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2065)
at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:2002)
at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:3277)
at androidx.fragment.app.FragmentManager.dispatchViewCreated(FragmentManager.java:3180)
at androidx.fragment.app.Fragment.performViewCreated(Fragment.java:3153)
at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:608)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:286)
at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2164)
at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2065)
at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:2002)
at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:3277)
at androidx.fragment.app.FragmentManager.dispatchActivityCreated(FragmentManager.java:3187)
at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:263)
at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:350)
at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:251)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1582)
at android.app.Activity.performStart(Activity.java:8628)
at android.app.ActivityThread.handleStartActivity(ActivityThread.java:3807)

iOS Simulator exception
Unhandled Exception:
System.ArgumentException: Arg_ArgumentException Arg_ParamName_Name, serviceProvider
   at CommunityToolkit.Maui.Extensions.AppThemeResourceExtension.ProvideValue(IServiceProvider serviceProvider)
   at MauiApp1.MainPage.InitializeComponent()
   at MauiApp1.MainPage..ctor()
   at System.Reflection.ConstructorInvoker.InterpretedInvoke(Object obj, IntPtr* args)
   at System.Reflection.ConstructorInvoker.InvokeDirectByRefWithFewArgs(Span`1 copyOfArgs)
   at System.Reflection.ConstructorInvoker.InvokeImpl(Object arg1, Object arg2, Object arg3, Object arg4)
   at System.Reflection.ConstructorInvoker.Invoke(Span`1 arguments)
   at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.ConstructorMatcher.CreateInstance(IServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.CreateInstance(IServiceProvider provider, Type instanceType, Object[] parameters)
   at Microsoft.Maui.Controls.ShellContent.<>c__DisplayClass20_0.<Microsoft.Maui.Controls.IShellContentController.GetOrCreateContent>b__0()
   at Microsoft.Maui.Controls.ElementTemplate.CreateContent()
   at Microsoft.Maui.Controls.Internals.DataTemplateExtensions.CreateContent(DataTemplate self, Object item, BindableObject container)
   at Microsoft.Maui.Controls.ShellContent.Microsoft.Maui.Controls.IShellContentController.GetOrCreateContent()
   at Microsoft.Maui.Controls.Platform.Compatibility.ShellSectionRootRenderer.LoadRenderers()
   at Microsoft.Maui.Controls.Platform.Compatibility.ShellSectionRootRenderer.ViewDidLoad()
--- End of stack trace from previous location ---
   at ObjCRuntime.Runtime.ThrowException(IntPtr gchandle)
   at UIKit.UIApplication.UIApplicationMain(Int32 argc, String[] argv, IntPtr principalClassName, IntPtr delegateClassName)
   at UIKit.UIApplication.Main(String[] args, Type principalClass, Type delegateClass)
   at MauiApp1.iOS.Program.Main(String[] args)

Expected Behavior
AppThemeResourceExtension does not throw an exception.
Steps To Reproduce
Open and run solution from reproduction repository
Link to public reproduction project repository
https://github.com/DooNeGo/MauiApp1
Environment
- .NET MAUI CommunityToolkit: 10.0.0
- OS: Windows 10 Build 19045.5247, iOS Simulator 18.2, Android Emulator Api 34
- .NET MAUI: 9.0.21
Anything else?
No response

Duplicate issue #2431: [BUG] AppThemeResourceExtension crash in Release mode: Value does not fall within the expected range. (Parameter 'serviceProvider')
Created: 2025-01-08T19:01:25Z
Is there an existing issue for this?

 I have searched the existing issues

Did you read the "Reporting a bug" section on Contributing file?

 I have read the "Reporting a bug" section on Contributing file: https://github.com/CommunityToolkit/Maui/blob/main/CONTRIBUTING.md#reporting-a-bug

Current Behavior
With version 10.0.0 CommunityToolkit there is some change for AppThemeResourceExtension that causes only release build to crash. Debug builds run fine.
Error
Exception has occurred: System.ArgumentException
Value does not fall within the expected range. (Parameter 'serviceProvider')
    at CommunityToolkit.Maui.Extensions.AppThemeResourceExtension.ProvideValue(IServiceProvider serviceProvider)
    at MauiAppXyz.MainPage.InitializeComponent()
    at MauiAppXyz.MainPage..ctor()
    at System.Reflection.ConstructorInvoker.InterpretedInvoke(Object obj, IntPtr* args)
    at System.Reflection.ConstructorInvoker.InvokeDirectByRefWithFewArgs(Span`1 copyOfArgs)
    at System.Reflection.ConstructorInvoker.InvokeImpl(Object arg1, Object arg2, Object arg3, Object arg4)
    at System.Reflection.ConstructorInvoker.Invoke(Span`1 arguments)
    at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.ConstructorMatcher.CreateInstance(IServiceProvider provider)
    at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.CreateInstance(IServiceProvider provider, Type instanceType, Object[] parameters)
    at Microsoft.Maui.Controls.ShellContent.<>c__DisplayClass20_0.<Microsoft.Maui.Controls.IShellContentController.GetOrCreateContent>b__0()
    at Microsoft.Maui.Controls.ElementTemplate.CreateContent()
    at Microsoft.Maui.Controls.Internals.DataTemplateExtensions.CreateContent(DataTemplate self, Object item, BindableObject container)
    at Microsoft.Maui.Controls.ShellContent.Microsoft.Maui.Controls.IShellContentController.GetOrCreateContent()
    at Microsoft.Maui.Controls.Platform.Compatibility.ShellSectionRootRenderer.LoadRenderers()
    at Microsoft.Maui.Controls.Platform.Compatibility.ShellSectionRootRenderer.ViewDidLoad()
    at Microsoft.Maui.Controls.Platform.Compatibility.ShellSectionRootRenderer.__Registrar_Callbacks__.callback_131_Microsoft_Maui_Controls_Platform_Compatibility_ShellSectionRootRenderer_ViewDidLoad(IntPtr pobj, IntPtr sel, IntPtr* exception_gchandle)

Here is the reproducible sample
pmahend1/maui-toolkit-appresource-issue
It is most likely due to recent change [ContentProperty(nameof(Key)), RequireService([typeof(IServiceProvider)])]  in AppThemeResourceExtension. I have tried registering IServiceProvider with default ServiceProvider and tried registering and resolving AppShell as mentioned in dependency-injection/limitations-with-xaml-resources
Work-around
Reverted nuget to 9.1.1
Expected Behavior
AppThemeResourceExtension should run fine in both debug and release configurations.
Steps To Reproduce

Clone pmahend1/maui-toolkit-appresource-issue main branch.
Run it on Debug mode on either iOS or Android
Run it on Release mode on either iOS or Android

Link to public reproduction project repository
https://github.com/pmahend1/maui-toolkit-appresource-issue
Environment
- .NET MAUI CommunityToolkit: 10.0.0
- OS: NA but on macOS Sequoia 15.2
- .NET MAUI: Dotnet 9.0.101 , MAUI workload : 9.0.100-manifests.1e472b70
Anything else?
Reverting to v9.1.1 throws tens of warnings for XAML compiled binding and not having required-service everywhere

Closing comment (2025-01-08T20:50:23Z): Found that it is duplicate of #2415