question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Multiple controls don't support theme changes properly on the fly

See original GitHub issue

Description

If you try to change the app theme when a MAUI application is running, multiple controls will not be drawn properly. At least not on Android and Windows. I have created a sample application with all these controls:

  • Search bar
  • Toolbar menu
  • Radio buttons
  • Picker
  • Checkbox
  • Switch control
  • Two buttons. Both are enabled, but the first one has been temporary disabled during launch. Both these should be drawn identical.

Here, I start the application on Android where the system theme is Light. Then I force the app to dark theme, and then to light theme.

image

Another sample on Android, but the system theme is Dark:

image

Here on Windows 11, the system theme is Light:

image

Here on Window 11, the system theme is Dark.

image

I see these issues:

  • Search Box: On Android, the search box always uses the system colors. Even if I force my app by setting UserAppTheme to a specific theme, the system colors will be used. On Windows, the search box could be invisible.
  • Toolbar menu: Always uses the color that was set on launch. Never changes on the fly. This is already reported, and there is a workaround for Android. See issue #8756.
  • Radio buttons: The rings could disappear. On Windows also the text could disappear.
  • Picker: On Andriod, the line in the picker could disappear. On Windows the border could disappear. I have also seen the text disappear but appear again when hovering in Windows.
  • Checkbox: The border can disappear. And appear again with a different color.
  • Switch control. Sometimes invisible. On Windows, the text could disappear.
  • Buttons that have been disabled and then enabled again cannot change theme. This has been reported before, see #9220.

Steps to Reproduce

  1. Create a new MAUI application.
  2. Update MainPags.xaml.cs:
namespace MauiIssues;

public partial class MainPage : ContentPage
{
	public MainPage()
	{
		InitializeComponent();

        Picker.ItemsSource = new List<string> { "One", "Two", "Three" };
		Picker.SelectedIndex = 1;
    }

    override async protected void OnAppearing()
    {
        base.OnAppearing();

        DisabledButton.IsEnabled = false;
        await Task.Delay(100);
        DisabledButton.IsEnabled = true;

        Window.Width = 360;
        Window.Height = 700;
    }

    private void ThemeLight_CheckedChanged(object sender, CheckedChangedEventArgs e)
    {
        if (ThemeLight.IsChecked)
        {
            App.Current.UserAppTheme = AppTheme.Light;
        }
        else
        {
            App.Current.UserAppTheme = AppTheme.Dark;            
        }
    }
}
  1. Update MainPage.xaml:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MauiIssues.MainPage">
    <ContentPage.ToolbarItems>
        <ToolbarItem Order="Secondary" Text="Secondary 1" />
    </ContentPage.ToolbarItems>
    <Shell.SearchHandler>
        <SearchHandler 
                Placeholder="Search box"
                SearchBoxVisibility="Expanded"
                >
        </SearchHandler>
    </Shell.SearchHandler>

    <ScrollView>
        <VerticalStackLayout 
            Spacing="12" 
            Padding="30" 
            VerticalOptions="Start">

            <Label Text="Theme" />
            <HorizontalStackLayout>
                <RadioButton x:Name="ThemeLight" Content="Light" CheckedChanged="ThemeLight_CheckedChanged" />
                <RadioButton x:Name="ThemeDark" Content="Dark" CheckedChanged="ThemeLight_CheckedChanged" />
            </HorizontalStackLayout>

            <Label Text="Picker:" />
            <Picker x:Name="Picker" />

            <Label Text="Check box:" />
            <CheckBox HorizontalOptions="Start" />

            <Label Text="Switch:" />
            <Switch HorizontalOptions="Start" />

            <Label Text="Reenabled button:" />
            <Button Text="Hello world" x:Name="DisabledButton" />

            <Label Text="Button:" />
            <Button Text="Hello world" />
        </VerticalStackLayout>
    </ScrollView>
</ContentPage>
  1. Run the application and see what happens when the theme is changed.

Link to public reproduction project repository

https://github.com/pekspro/MauiIssues/tree/11849_Multiple_controls_dont_support_theme_changes

Version with bug

7.0 (current)

Last version that worked well

Unknown/Other

Affected platforms

Android, Windows, I was not able test on other platforms

Affected platform versions

Windows 11, Android 11, Android 13.

Did you find any workaround?

No response

Relevant log output

No response

Issue Analytics

  • State:open
  • Created 10 months ago
  • Reactions:5
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
breenbobcommented, Mar 13, 2023

We are also affected by these issues.

0reactions
jinxinjuancommented, Jul 17, 2023

Verified this issue with Visual Studio Enterprise 17.7.0 Preview 3.0(net8). Can repro on windows and android platform with sample project. 11849_Multiple_controls_dont_support_theme_changes

Read more comments on GitHub >

github_iconTop Results From Across the Web

Multiple controls don't support theme changes properly on ...
NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop. - Multiple controls don't support theme ......
Read more >
Theme an app - .NET MAUI
.NET Multi-platform App UI (.NET MAUI) apps can respond to style changes dynamically at runtime by using the DynamicResource markup extension.
Read more >
Handle configuration changes
Handle configuration changes in your Android app. ... language, or preferred theme changes their respective values in the Configuration object.
Read more >
Styles and themes | Android Developers
Styles and themes on Android let you separate the details of your app design from the UI structure and behavior, similar to stylesheets...
Read more >
Just-in-Time Mode
If your CSS seems to be rebuilding in an infinite loop, there's a good chance it's because your build tool doesn't support PostCSS's...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found