dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22.27k stars 1.76k forks source link

Labels disappear on Windows when switching theme #24950

Open BryanLanning opened 2 months ago

BryanLanning commented 2 months ago

Description

When switching the theme on a Windows device, the Labels disappear. On an Android device or emulator, the Labels' styles are correctly updated.

Steps to Reproduce

  1. Create a File > New .NET MAUI App.
  2. Change the minimum supported version for the Windows target to 10.0.20348.0.
  3. Add CommunityToolkit.Maui and CommunityToolkit.MVVM NuGet packages.
  4. Add the WindowsSdkPackageVersion node to the .csproj file. This is to fix the following error from the MVVM toolkit:

    MVVMTKCFG0003 This version of the MVVM Toolkit requires 'Microsoft.Windows.SDK.NET.Ref' version '10.0.20348.38' or later. Please update to .NET SDK 8.0.109, 8.0.305 or 8.0.402 (or later). Alternatively, use a temporary 'Microsoft.Windows.SDK.NET.Ref' reference, which can be done by setting the 'WindowsSdkPackageVersion' property in your .csproj file. For your project configuration, it is recommended to set the package version to '10.0.20348.41'. MauiThemeLabelsVanish (net8.0-windows10.0.20348.0) S:\packages\nuget\communitytoolkit.mvvm\8.3.2\buildTransitive\CommunityToolkit.Mvvm.WindowsSdk.targets Reference.

    <PropertyGroup>
    <!-- nuget package: Microsoft.Windows.SDK.NET.Ref -->
    <WindowsSdkPackageVersion>10.0.20348.45</WindowsSdkPackageVersion>
    </PropertyGroup>
  5. Add the INavigationService and MauiNavigationService as seen in the Navigation section of Microsoft Learn's Enterprise Application Patterns Using .NET MAUI.
  6. Add a MainView with several types of controls, and a ThemeSelectionView to change the theme. ViewModels, dependency-injection registration in MauiProgram and route registration in AppShell.xaml.cs as well.
  7. Add a ThemeService and Theme dictionaries for Default/Red themes. Copy the content of Colors.xaml and Styles.xaml (from Resources/Styles) into both DefaultTheme.xaml and RedTheme.xaml.
    1. Change the Primary color for the RedTheme to Red.
    2. Change the Secondary color for the RedTheme to Pink.
    3. Change the Secondary color for the DefaultTheme to LightBlue.
  8. Change the StaticResources for the TargetType="Label" in both Theme.xaml files from StaticResource to DynamicResource.
  9. Implement Theme switching as seen in Microsoft's Learn documentation for .NET MAUI 8 at Theming→Respond To system theme changes.
  10. Add WeakMessage registration to AppShell to listen for changes to the theme.

Link to public reproduction project repository

https://github.com/BryanLanning/MauiThemeLabelsVanish

Version with bug

8.0.91 SR9.1

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

Windows

Affected platform versions

Windows SDK 10.0.20348.38

Did you find any workaround?

Option 1

Remove the BackgroundColor Setter from all TargetType="Label" styles. Including ones with an x:Key (Headline and SubHeadline) and both DefaultTheme and RedTheme. Even changing it to something other than Transparent does not work.

Option 2

Move all TargetType="Label" styles from the Theme.xaml files to the App.xaml file inside the ResourceDictionary xml element, below the reference to the DefaultTheme.xaml.

Relevant log output

No response

ninachen03 commented 2 months ago

I can repro this issue at windows platform on the latest 17.12.0 Preview 2.0(8.0.71 & 8.0.91).

MartyIX commented 1 month ago

It would be great to figure out when this issue was introduced. I think i can see the same issue on my side too and I think it started with MAUI 8.0.90 but I can't check it out at the moment.