Skip to content

[BUG] AppThemeResourceExtension crashes the app after updating the community toolkit version to 10.0.0 #2415

Closed

Description

@DooNeGo

Is there an existing issue for this?

  • I have searched the existing issues

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

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

Activity

  1. yurkinh commented on Jan 2, 2025

    @yurkinh

    The same happens for me. Both platforms

  2. malsabi commented on Jan 12, 2025

    @malsabi

    I'm also having this issue on Windows & Android. Reverting back to 9.1.1 resolves the issue.

  3. baaaaif commented on Jan 14, 2025

    @baaaaif

    +1
    same here

  4. zwikk commented on Jan 17, 2025

    @zwikk

    Same. Not fixed in 11.0.0 😫

  5. pmahend1 commented on Jan 17, 2025

    @pmahend1

    This issue has not been triaged yet. Someone needs to do it and create PR.

  6. baaaaif commented on Jan 20, 2025

    @baaaaif

    Maybe you need to adjust the title and mention that the app crashes and that it's not just an exception that could be caught.

    I'm a bit surprised that this doesn't get any attention from the maintainers. Sorry @jfversluis - I'll just tag you as a managing maintainer.

  7. changed the title [-][BUG] AppThemeResourceExtension throws exception in Release after updating community toolkit version to 10.0.0[/-] [+][BUG] AppThemeResourceExtension crashes the app after updating the community toolkit version to 10.0.0[/+] on Jan 20, 2025
  8. jfversluis commented on Jan 20, 2025

    @jfversluis
    Member

    @pmahend1 or anyone here, feel free to do so 😉 Its a community project, we're all in this together!

    Quick investigation shows its because this path is invoked:

    https://github.com/CommunityToolkit/Maui/blob/main/src/CommunityToolkit.Maui/Extensions/AppThemeResourceExtension.shared.cs#L27-L30

    More specifically: serviceProvider.GetService(typeof(IProvideValueTarget)) is null so that's why it breaks. Now what is the fix, that is the question.

  9. jfversluis commented on Jan 20, 2025

    @jfversluis
    Member

    Oh found it, it wasn't actually that hard.

    Could you try the resulting build from #2460 to verify that fixes it for you?

  10. added
    area/essentialsIssue/Discussion/PR that has to do with Essentials
    area/extensionsIssue/Discussion/PR that has to do with Extensions
    and removed on Jan 20, 2025
  11. baaaaif commented on Jan 20, 2025

    @baaaaif

    I can confirm that it fixes the problem for me on Android
    big thx Gerald

  12. DooNeGo commented on Jan 20, 2025

    @DooNeGo
    Author

    I can also confirm the fix, it works for me on Android and Windows.
    thx Gerald

  13. pmahend1 commented on Jan 20, 2025

    @pmahend1

    @jfversluis Thanks for the fix. I was trying to find a fix but was not able to. I did not know about typeof(IProvideParentValues) 😄

  14. locked and limited conversation to collaborators on Jan 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/essentialsIssue/Discussion/PR that has to do with Essentialsarea/extensionsIssue/Discussion/PR that has to do with ExtensionsbugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions