Skip to content

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

@muni20

Description

@muni20

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions