Bug Report: App Crashes on Theme Change When Multiple Tabs Are Open in TabView
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
- create a WinUI app
- implement TabView with settings page where you can change the theme to dark or light
- change the theme while multiple tabs are open
- 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
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?
Hi @muni20 , can you provide minimal repro project for the above issue?
@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:
- Clone and run the app.
- Open two tabs (the app has a tabbed interface).
- Go to the Settings tab in one of the tabs.
- Change the App Theme from Light to Dark or vice versa.
- Navigate to another page from Settings (e.g., Home or any other page ** All are blankpage1 or blankpage2**).
- 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
@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:
- Clone and run the app.
- Open two tabs (the app has a tabbed interface).
- Go to the Settings tab in one of the tabs.
- Change the App Theme from Light to Dark or vice versa.
- Navigate to another page from Settings (e.g., Home or any other page ** All are blankpage1 or blankpage2**).
- Switch to the other tab.
❗ Bug Observed:
- App crashes
🔍 Root Cause Analysis (Likely)
WinUI 3's
FrameworkElement.RequestedThemeorApplication.RequestedThememay not propagate correctly across multiple Frame instances or tab contents if they were initialized before the theme change.Each tab probably uses its own
Frameand 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.
But if I select the new tab right after adding it and then do the rest, a crash will occur.
@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