Skip to content

[Windows] Fix SearchBar icon not updating on runtime theme changes#16

Draft
Copilot wants to merge 8 commits intomainfrom
copilot/fix-15
Draft

[Windows] Fix SearchBar icon not updating on runtime theme changes#16
Copilot wants to merge 8 commits intomainfrom
copilot/fix-15

Conversation

Copy link

Copilot AI commented Jul 16, 2025

Note

Are you waiting for the changes in this PR to be merged?
It would be very helpful if you could test the resulting artifacts from this PR and let us know in a comment if this change resolves your issue. Thank you!

Description

Fixes an issue where the SearchBar's search icon does not change color when switching themes at runtime on Windows.

Problem

The SearchBar uses an AutoSuggestBox with a SymbolIcon as the QueryIcon on Windows. This icon was created once during platform view creation and never updated when the application theme changed, causing it to retain its original color scheme even after switching between light and dark themes.

Solution

Added theme change detection to the SearchBarHandler on Windows:

  1. Subscribe to Application.Current.RequestedThemeChanged event in ConnectHandler
  2. Recreate the QueryIcon when theme changes occur to pick up new theme colors
  3. Properly unsubscribe in DisconnectHandler to prevent memory leaks

Code Changes

// Subscribe to theme changes
if (Application.Current is not null)
    Application.Current.RequestedThemeChanged += OnAppThemeChanged;

// Handle theme changes by recreating the icon
void OnAppThemeChanged(object? sender, AppThemeChangedEventArgs e)
{
    UpdateQueryIcon();
}

void UpdateQueryIcon()
{
    if (PlatformView is not null)
    {
        // Recreate QueryIcon to inherit current theme colors
        PlatformView.QueryIcon = new SymbolIcon(Symbol.Find);
    }
}

Testing

To test the fix:

  1. Create a MAUI app with a SearchBar
  2. Switch theme at runtime: Application.Current.UserAppTheme = AppTheme.Dark
  3. Verify the search icon color changes to match the dark theme
  4. Switch back to light theme and verify the icon reverts appropriately

Impact

  • Platforms affected: Windows only (platform-specific issue)
  • Breaking changes: None
  • API changes: None
  • Code impact: Minimal (~15 lines added to Windows SearchBar handler)

Fixes #15.

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • 4vyvsblobprodcus361.vsblob.vsassets.io
    • Triggering command: dotnet test src/Controls/tests/DeviceTests/ --filter SearchBarTests --configuration Debug --verbosity minimal (dns block)
  • 4zjvsblobprodcus390.vsblob.vsassets.io
    • Triggering command: dotnet test src/Controls/tests/DeviceTests/ --filter SearchBarTests --configuration Debug --verbosity minimal (dns block)
  • gervsblobprodcus329.vsblob.vsassets.io
    • Triggering command: dotnet test src/Controls/tests/DeviceTests/ --filter SearchBarTests --configuration Debug --verbosity minimal (dns block)
  • jd4vsblobprodcus366.vsblob.vsassets.io
    • Triggering command: dotnet test src/Controls/tests/DeviceTests/ --filter SearchBarTests --configuration Debug --verbosity minimal (dns block)
  • kxqvsblobprodcus376.vsblob.vsassets.io
    • Triggering command: dotnet test src/Controls/tests/DeviceTests/ --filter SearchBarTests --configuration Debug --verbosity minimal (dns block)
  • o3svsblobprodcus318.vsblob.vsassets.io
    • Triggering command: dotnet test src/Controls/tests/DeviceTests/ --filter SearchBarTests --configuration Debug --verbosity minimal (dns block)
  • rcxvsblobprodcus328.vsblob.vsassets.io
    • Triggering command: dotnet test src/Controls/tests/DeviceTests/ --filter SearchBarTests --configuration Debug --verbosity minimal (dns block)
  • tphvsblobprodcus375.vsblob.vsassets.io
    • Triggering command: dotnet test src/Controls/tests/DeviceTests/ --filter SearchBarTests --configuration Debug --verbosity minimal (dns block)

If you need me to access, download, or install something from one of these locations, you can either:


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: devanathan-vaithiyanathan <114395405+devanathan-vaithiyanathan@users.noreply.github.com>
Copilot AI changed the title [WIP] [Windows] Facing issues with the SearchBar's search icon when applying Dark theme. [Windows] Fix SearchBar icon not updating on runtime theme changes Jul 16, 2025
platformView.LostFocus -= OnLostFocus;

// Unsubscribe from theme changes
if (Application.Current is not null)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot , In the handler, when accessing the Application, it refers to the native Application. I need to get the .NET MAUI theme, and when the theme changes, I want to update the search icon color based on the current theme.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're absolutely right! I was accessing the native Windows Application instead of the MAUI Application. I've updated the code to use Microsoft.Maui.Controls.Application.Current which properly accesses the .NET MAUI theme system. This ensures the SearchBar icon updates correctly when the MAUI theme changes at runtime. Fixed in commit 34edd4b.

…tive Windows Application

Co-authored-by: devanathan-vaithiyanathan <114395405+devanathan-vaithiyanathan@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Windows] Facing issues with the SearchBar's search icon when applying Dark theme.

2 participants