Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?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="Maui.Controls.Sample.Issues.SearchBarThemeIssue"
Title="SearchBar Theme Issue Test">

<StackLayout Padding="20">
<Label Text="Test: SearchBar icon should change color when switching themes at runtime"
FontSize="16"
Margin="0,0,0,20"/>

<SearchBar x:Name="testSearchBar"
Text="Search"
Placeholder="Enter search text"
WidthRequest="400"
Margin="0,0,0,20"/>

<Button x:Name="changeThemeButton"
Text="Switch to Dark Theme"
WidthRequest="400"
Clicked="OnChangeThemeClicked"
Margin="0,0,0,10"/>

<Button x:Name="resetThemeButton"
Text="Switch to Light Theme"
WidthRequest="400"
Clicked="OnResetThemeClicked"
Margin="0,0,0,20"/>

<Label x:Name="statusLabel"
Text="Current theme: Light"
FontSize="14"/>
</StackLayout>

</ContentPage>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace Maui.Controls.Sample.Issues
{
public partial class SearchBarThemeIssue : ContentPage
{
public SearchBarThemeIssue()
{
InitializeComponent();
}

private void OnChangeThemeClicked(object sender, EventArgs e)
{
Application.Current.UserAppTheme = AppTheme.Dark;
statusLabel.Text = "Current theme: Dark";
}

private void OnResetThemeClicked(object sender, EventArgs e)
{
Application.Current.UserAppTheme = AppTheme.Light;
statusLabel.Text = "Current theme: Light";
}
}
}
23 changes: 23 additions & 0 deletions src/Core/src/Handlers/SearchBar/SearchBarHandler.Windows.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#nullable enable
using Microsoft.UI.Xaml.Controls;
using Microsoft.Maui.Controls;

namespace Microsoft.Maui.Handlers
{
Expand All @@ -24,6 +25,10 @@ protected override void ConnectHandler(AutoSuggestBox platformView)
// To address this, I have specifically handled the focus and unfocus events for AutoSuggestBox here.
platformView.GotFocus += OnGotFocus;
platformView.LostFocus += OnLostFocus;

// Subscribe to theme changes to update QueryIcon
if (Microsoft.Maui.Controls.Application.Current is not null)
Microsoft.Maui.Controls.Application.Current.RequestedThemeChanged += OnAppThemeChanged;
}

protected override void DisconnectHandler(AutoSuggestBox platformView)
Expand All @@ -33,6 +38,10 @@ protected override void DisconnectHandler(AutoSuggestBox platformView)
platformView.TextChanged -= OnTextChanged;
platformView.GotFocus -= OnGotFocus;
platformView.LostFocus -= OnLostFocus;

// Unsubscribe from theme changes
if (Microsoft.Maui.Controls.Application.Current is not null)
Microsoft.Maui.Controls.Application.Current.RequestedThemeChanged -= OnAppThemeChanged;
}

public static void MapBackground(ISearchBarHandler handler, ISearchBar searchBar)
Expand Down Expand Up @@ -166,5 +175,19 @@ void OnLostFocus(object sender, UI.Xaml.RoutedEventArgs e)
{
UpdateIsFocused(false);
}

void OnAppThemeChanged(object? sender, AppThemeChangedEventArgs e)
{
UpdateQueryIcon();
}

void UpdateQueryIcon()
{
if (PlatformView is not null)
{
// Update the QueryIcon to ensure it reflects the current theme
PlatformView.QueryIcon = new SymbolIcon(Symbol.Find);
}
}
}
}