Maui icon indicating copy to clipboard operation
Maui copied to clipboard

[BUG] AppThemeColor Resource not found for key in v12.0.0

Open JacobGralinski opened this issue 4 months ago • 4 comments
trafficstars

Is there an existing issue for this?

  • [x] I have searched the existing issues

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

  • [x] 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

I have a few different themes for my .NET MAUI app that are used at different times depending on the situation which works as expected in v11.2.0, but after upgrading to v12.0.0 I immediately started hitting "Resource not found for key" errors on startup even though I didn't change anything about my App.xaml, App.xaml.cs or the BaseTheme.xaml and BaseTheme.xaml.cs.

App.xaml

<Application 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"
             xmlns:local="clr-namespace:GMA.Application.Mobile"
             x:Class="Application.Mobile.App">
    <Application.Resources>
        <ResourceDictionary Source="Resources/Themes/BaseTheme.xaml">
            <ActivityIndicator x:Key="GlobalActivityIndicator" IsRunning="False" IsVisible="False" Color="Blue" />
            <toolkit:IsStringNotNullOrEmptyConverter x:Key="IsStringNotNullOrEmptyConverter" />
            <toolkit:IsEqualConverter x:Key="IsEqualConverter" />
            <toolkit:IsNotNullConverter x:Key="IsNotNullConverter" />
            <toolkit:ByteArrayToImageSourceConverter x:Key="ByteArrayToImageSourceConverter" />
        </ResourceDictionary>
    </Application.Resources>
</Application>

App.xaml.cs

public static ResourceDictionary BaseTheme { get; set; }

public App()
 {
     InitializeComponent();

     BaseTheme = new BaseTheme();

     SetBaseTheme(App.Current.PlatformAppTheme);

     App.Current.RequestedThemeChanged += (sender, args) =>
     {
         SetBaseTheme(args.RequestedTheme);
     };

     AppDomain.CurrentDomain.UnhandledException += (sender, e) =>
     {
         var ex = e.ExceptionObject as Exception;

         Log.Fatal(e.ExceptionObject as Exception, ex.Message);
         Log.CloseAndFlush();
     };

     TaskScheduler.UnobservedTaskException += (sender, e) =>
     {
         Log.Fatal(e.Exception, e.Exception.Message);
         Log.CloseAndFlush();
         e.SetObserved();
     };
 }

private void SetBaseTheme(AppTheme appTheme)
{
 Resources.MergedDictionaries.Clear();
 ICollection<ResourceDictionary> mergedDictionaries = App.Current.Resources.MergedDictionaries;

 if (mergedDictionaries != null)
 {
     mergedDictionaries.Clear();
     mergedDictionaries.Add(BaseTheme);
 }
}

public static void SetTheme(ResourceDictionary theme)
{
 ICollection<ResourceDictionary> mergedDictionaries = App.Current.Resources.MergedDictionaries;

 if (mergedDictionaries != null)
 {
     mergedDictionaries.Clear();
     mergedDictionaries.Add(theme);
 }
}

BaseTheme.xaml

<ResourceDictionary 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"
             x:Class="Application.Mobile.Resources.Themes.BaseTheme">

    <toolkit:AppThemeColor Light="#3E805C" Dark="#E0D5C5" x:Key="ActivityIndicatorColor" />
    <toolkit:AppThemeColor Light="#C8C8C8" Dark="#6E6E6E" x:Key="IndicatorViewColor" />
    <toolkit:AppThemeColor Light="#141414" Dark="#E1E1E1" x:Key="IndicatorViewSelectedColor" />

    <toolkit:AppThemeColor Light="#C8C8C8" Dark="#6E6E6E" x:Key="BorderStrokeColor" />

    <toolkit:AppThemeColor Light="#141414" Dark="#C8C8C8" x:Key="BoxViewColor" />

    <Color x:Key="White">#E0D5C5</Color>
    <Color x:Key="Black">Black</Color>

    <Style TargetType="ActivityIndicator">
        <Setter Property="Color" Value="{toolkit:AppThemeResource ActivityIndicatorColor}" />
    </Style>

    <Style TargetType="IndicatorView">
        <Setter Property="IndicatorColor" Value="{toolkit:AppThemeResource IndicatorViewColor}"/>
        <Setter Property="SelectedIndicatorColor" Value="{toolkit:AppThemeResource IndicatorViewSelectedColor}"/>
    </Style>

    <Style TargetType="Border">
        <Setter Property="Stroke" Value="{toolkit:AppThemeResource BorderStrokeColor}" />
        <Setter Property="StrokeShape" Value="Rectangle"/>
        <Setter Property="StrokeThickness" Value="1"/>
    </Style>
</ResourceDictionary>

Expected Behavior

I expect the AppThemeColor elements to be loaded how they were in v11.2.0 and for the Keys to be appropriately found

Steps To Reproduce

Update the Community Toolkit NuGet package from v11.2.0 to v12.0.0

Link to public reproduction project repository

https://github.com/JacobGralinski/ToolkitThemesIssue

Environment

- .NET MAUI CommunityToolkit: 12.0.0
- OS: Windows 11
- .NET MAUI: 9.0.100

Anything else?

I looked at the release notes for v12 and didn't see anything immediate that would've broken AppThemeColor functionality. Maybe I'm doing something wrong with themes. Any help would be great.

JacobGralinski avatar Jun 23 '25 22:06 JacobGralinski

There was this change which may have contributed to this https://github.com/CommunityToolkit/Maui/pull/2639

bijington avatar Jun 24 '25 05:06 bijington

That looks to be what's causing this. Based on the error message I'm getting which is only "Resource not found for key {key}", my code is getting all the way through the method without passing any of the Find/TryGet conditionals. Not sure why that would be the case though.

JacobGralinski avatar Jun 24 '25 13:06 JacobGralinski

@jfversluis might have an idea?

bijington avatar Jun 24 '25 19:06 bijington

I'll have to look into this, the repro will be very helpful for that. Thank you!

jfversluis avatar Jun 26 '25 09:06 jfversluis

Hi,

Unfortunately, I'm also experiencing this problem after upgrading my project to v12.1.0 from 11.2. After spending a lot of time converting the Popup v2 handling, I've run into this problem, It's quite frustrating... Is there a workaround I can use to avoid having to revert to v11.2 ?

Thank you so much!

gaglia avatar Jul 23 '25 15:07 gaglia

A workaround would probably be to bring in the AppThemeResource implementation to your codebase and fix the issue. If you do manage to fix it I would ask that you send a PR back to this toolkit to help resolve it for others

bijington avatar Jul 23 '25 18:07 bijington

Hi Shaun / Gerald, this bug is affecting my projects as well, so I've had a quick look and the problem is the rootobject is a RescourceDictionary, not a IResourceProvider. The code changes below work, but I realise that the problem may need fixing in maui itself, so I haven't done a PR for it.

In CommunityToolkit.Maui\Extensions.AppThemeResourceExtension.shared.cs ...

	// Fallback to root object ResourceDictionary (e.g. page-level resources)
	var rootProvider = serviceProvider.GetService(typeof(IRootObjectProvider)) as IRootObjectProvider;
	var root = rootProvider?.RootObject;

	// root is a ResourceDictionary, not an IResourcesProvider

	//if (root is IResourcesProvider { IsResourcesCreated: true } rootResources
	//	&& rootResources.Resources.TryGetValue(Key, out resource))

	// so change to:
	if (root is ResourceDictionary rootResources
		&& rootResources.TryGetValue(Key, out resource))

	// probably needs a fix to IRootObjectProvider service really?

hth, David.

misterspeak avatar Oct 26 '25 13:10 misterspeak