Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
2 tasks done
DooNeGo opened this issue Dec 26, 2024 · 11 comments · Fixed by #2460
Closed
2 tasks done
Labels
area/essentials Issue/Discussion/PR that has to do with Essentials area/extensions Issue/Discussion/PR that has to do with Extensions bug Something isn't working

Comments

@DooNeGo
Copy link

DooNeGo commented Dec 26, 2024

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

@DooNeGo DooNeGo added bug Something isn't working unverified labels Dec 26, 2024
@yurkinh
Copy link

yurkinh commented Jan 2, 2025

The same happens for me. Both platforms

@malsabi
Copy link

malsabi commented Jan 12, 2025

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

@baaaaif
Copy link

baaaaif commented Jan 14, 2025

+1
same here

@zwikk
Copy link

zwikk commented Jan 17, 2025

Same. Not fixed in 11.0.0 😫

@pmahend1
Copy link

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

@baaaaif
Copy link

baaaaif commented Jan 20, 2025

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.

@DooNeGo DooNeGo 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 Jan 20, 2025
@jfversluis
Copy link
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.

@jfversluis
Copy link
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?

@jfversluis jfversluis added area/essentials Issue/Discussion/PR that has to do with Essentials area/extensions Issue/Discussion/PR that has to do with Extensions and removed unverified labels Jan 20, 2025
@baaaaif
Copy link

baaaaif commented Jan 20, 2025

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

@DooNeGo
Copy link
Author

DooNeGo commented Jan 20, 2025

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

@pmahend1
Copy link

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

@github-actions github-actions bot locked and limited conversation to collaborators Jan 22, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area/essentials Issue/Discussion/PR that has to do with Essentials area/extensions Issue/Discussion/PR that has to do with Extensions bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants