Skip to content

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

@JacobGralinski

Description

@JacobGralinski

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

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.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions