StaticResource not found for key .. when merging resource dictionaries

-- -- 787 Reputation points
2023-10-30T02:19:37.8033333+00:00

Hi

I have two resource dictionaries for light and dark themes;

<ResourceDictionary x:Class="MyApp.Resources.Styles.LightTheme"
                    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
                    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">

    <Color x:Key="BackgroundThemeColor">#F9F6FF</Color>
</ResourceDictionary>

<ResourceDictionary x:Class="MyApp.Resources.Styles.DarkTheme"
                    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
                    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">

    <Color x:Key="BackgroundThemeColor" x:Uid="Neutral-10">#1C1B1F</Color>
</ResourceDictionary>

In App.xaml I have merged the two resource dictionaries as well have created a style StyleFontMedium;

<Application
    x:Class="MyApp.App"
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Resources/Styles/LightTheme.xaml" />
                <ResourceDictionary Source="Resources/Styles/DarkTheme.xaml" />
            </ResourceDictionary.MergedDictionaries>

            <Style x:Key="StyleFontMedium" TargetType="Label">
                <Setter Property="FontFamily" Value="AvenirHeavy" />
                <Setter Property="TextColor" Value="{AppThemeBinding Light=#1C1B1F, Dark=#E6E1E5}" />
                <Setter Property="FontSize" Value="13" />
            </Style>
        </ResourceDictionary>
    </Application.Resources>
</Application>

In App.xaml code behind I have created the app shell and, after setting the theme, I am going to the Login page;

    public partial class App
    {
        public App()
        {
            InitializeComponent();

            Current.MainPage = new AppShell();

            SetTheme();

            Application.Current.RequestedThemeChanged += (s, e) =>
            {
                SetTheme();
            };

            return Shell.Current.GoToAsync("//LoginPage");
        }

        private static void SetTheme()
        {
            if (Application.Current.RequestedTheme == AppTheme.Light)
            {
                App.Current.Resources = new LightTheme();
            }
            else
            {
                App.Current.Resources = new DarkTheme();
            }
        }

    }

On Login page I have a label control;


<ContentPage     
    x:Class="MyApp.Views.LoginPage"     
    xmlns:local="clr-namespace:MyApp"  >

        <Grid>
                <Label 
                    Grid.Row="0"
                    FontAttributes="Bold"
                    Style="{StaticResource StyleFontMedium}"
                    FontSize="30"
                    HorizontalOptions="Center"
                    Text="Welcome"
                    TextColor="{StaticResource AccentColor}" />        
        </Grid>
</ContentPage>

The app works OK on local device but when it is installed from TestFlight it gives error;

StaticResource not found for key StyleFontMedium

How can I make the resource StyleFontMedium work globally?

Thanks

Regards

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,160 questions
{count} votes