microsoft-ui-xaml icon indicating copy to clipboard operation
microsoft-ui-xaml copied to clipboard

Bug Report: App Crashes on Theme Change When Multiple Tabs Are Open in TabView

Open muni20 opened this issue 7 months ago • 1 comments

Describe the bug

When multiple tabs are open in a TabView and the application theme or system theme is changed (either manually or automatically), the application crashes with the following error:

System.ArgumentException: Value does not fall within the expected range.

here is my code for theme change

try
{
    var currentTheme = (FrameworkElement)App.m_window.Content;

    if (App.m_window.Content is FrameworkElement rootElement)
    {
        rootElement.RequestedTheme = param;
        TitleBarHelper.UpdateTitleBar(param);
        var ThemeSaved = await _settings.SaveSettings(AppConstants.Theme_Key, param.ToString());

        if (ThemeSaved)
        {
            infoBar.Severity = InfoBarSeverity.Success;
            infoBar.Title = "Theme Changed";
            infoBar.Message = $"Theme has been changed to {param.ToString()}.";
            infoBar.IsIconVisible = true;
            infoBar.IsOpen = true;

            await Task.CompletedTask;
        }
    }

}
catch (Exception ex)
{
    infoBar.Severity = InfoBarSeverity.Error;
    infoBar.Title = "Error";
    infoBar.Message = $"An error occurred while changing the theme: {ex.Message}";
    infoBar.IsIconVisible = true;
    infoBar.IsOpen = true;

    await Task.CompletedTask;
}

i have tried to iterating through the tabs farme and update there Theme still no luck.

Steps to reproduce the bug

  1. create a WinUI app
  2. implement TabView with settings page where you can change the theme to dark or light
  3. change the theme while multiple tabs are open
  4. now try to switch the tab it will throw an error

Expected behavior

No response

Screenshots

No response

NuGet package version

None

Windows version

No response

Additional context

No response

muni20 avatar May 02 '25 07:05 muni20

Same issue here with TabViewItem containing Grid > SplitView > NavigationView structure, but the crash also occurs with simpler structures (even without NavigationView). Crash happens when switching tabs after theme change.

Any workarounds available?

diluculo avatar Jun 17 '25 08:06 diluculo

Hi @muni20 , can you provide minimal repro project for the above issue?

snigdha011997 avatar Jun 24 '25 09:06 snigdha011997

@snigdha011997 Hi, sorry for late reply

📝 Description of the Repo and Bug

🔗 Repo: [ThemeWinUIBug by @muni20](https://github.com/muni20/ThemeWinUIBug)

Steps to Reproduce the Issue:

  1. Clone and run the app.
  2. Open two tabs (the app has a tabbed interface).
  3. Go to the Settings tab in one of the tabs.
  4. Change the App Theme from Light to Dark or vice versa.
  5. Navigate to another page from Settings (e.g., Home or any other page ** All are blankpage1 or blankpage2**).
  6. Switch to the other tab.

Bug Observed:

  • App crashes

🔍 Root Cause Analysis (Likely)

WinUI 3's FrameworkElement.RequestedTheme or Application.RequestedTheme may not propagate correctly across multiple Frame instances or tab contents if they were initialized before the theme change.

Each tab probably uses its own Frame and visual tree, so:

  • Changing the theme only updates the active window/tab, and
  • The inactive tabs don't re-evaluate or refresh their theme automatically.

PS: -Its my guess

muni20 avatar Jul 02 '25 13:07 muni20

@snigdha011997 Hi, sorry for late reply

📝 Description of the Repo and Bug

🔗 Repo: [ThemeWinUIBug by @muni20](https://github.com/muni20/ThemeWinUIBug)

Steps to Reproduce the Issue:

  1. Clone and run the app.
  2. Open two tabs (the app has a tabbed interface).
  3. Go to the Settings tab in one of the tabs.
  4. Change the App Theme from Light to Dark or vice versa.
  5. Navigate to another page from Settings (e.g., Home or any other page ** All are blankpage1 or blankpage2**).
  6. Switch to the other tab.

Bug Observed:

  • App crashes

🔍 Root Cause Analysis (Likely)

WinUI 3's FrameworkElement.RequestedTheme or Application.RequestedTheme may not propagate correctly across multiple Frame instances or tab contents if they were initialized before the theme change.

Each tab probably uses its own Frame and visual tree, so:

  • Changing the theme only updates the active window/tab, and
  • The inactive tabs don't re-evaluate or refresh their theme automatically.

PS: -Its my guess

Your sample give me The parameter is incorrect.

Also, if I open a new tab without selecting it, then go to the settings and change theme, then navigate to another page and finally select that tab, everything works fine.

Image

But if I select the new tab right after adding it and then do the rest, a crash will occur.

Image

ghost1372 avatar Jul 02 '25 21:07 ghost1372

@ghost1372 yup i forgot to mention i copied some of the code from my main project, while debugging still no luck, and in my main project the first example thats working for you, isnt working, i guess its the children frame thats not changing its theme or something

https://github.com/user-attachments/assets/a981143b-c106-4f3d-96f7-fa6d74946ac0

muni20 avatar Jul 02 '25 21:07 muni20