diff --git a/src/Controls/tests/TestCases.Android.Tests/snapshots/android/VerifySearchBarPlaceholderAndBackgroundColor.png b/src/Controls/tests/TestCases.Android.Tests/snapshots/android/VerifySearchBarPlaceholderAndBackgroundColor.png new file mode 100644 index 000000000000..5364e2e99cc7 Binary files /dev/null and b/src/Controls/tests/TestCases.Android.Tests/snapshots/android/VerifySearchBarPlaceholderAndBackgroundColor.png differ diff --git a/src/Controls/tests/TestCases.HostApp/Issues/Issue29962.cs b/src/Controls/tests/TestCases.HostApp/Issues/Issue29962.cs new file mode 100644 index 000000000000..3c414edd1aef --- /dev/null +++ b/src/Controls/tests/TestCases.HostApp/Issues/Issue29962.cs @@ -0,0 +1,38 @@ +namespace Maui.Controls.Sample.Issues; + +[Issue(IssueTracker.Github, 29962, "[Windows] SearchBar PlaceHolder and Background Color should update properly at runtime", PlatformAffected.UWP)] +public class Issue29962 : ContentPage +{ + public Issue29962() + { + var searchBar = new SearchBar + { + Placeholder = "Search here...", + Margin = new Thickness(0, 30), + BackgroundColor = Colors.Black, + PlaceholderColor = Colors.Yellow, + HeightRequest = 50, + }; + var button = new Button + { + Text = "Change PlaceHolder and Background Color", + VerticalOptions = LayoutOptions.Center, + HorizontalOptions = LayoutOptions.Center, + AutomationId = "ColorChangeButton" + }; + button.Clicked += (sender, e) => + { + searchBar.BackgroundColor = Colors.YellowGreen; + searchBar.PlaceholderColor = Colors.Red; + }; + + Content = new StackLayout + { + Children = + { + searchBar, + button + } + }; + } +} \ No newline at end of file diff --git a/src/Controls/tests/TestCases.Mac.Tests/snapshots/mac/VerifySearchBarPlaceholderAndBackgroundColor.png b/src/Controls/tests/TestCases.Mac.Tests/snapshots/mac/VerifySearchBarPlaceholderAndBackgroundColor.png new file mode 100644 index 000000000000..01688d6b4409 Binary files /dev/null and b/src/Controls/tests/TestCases.Mac.Tests/snapshots/mac/VerifySearchBarPlaceholderAndBackgroundColor.png differ diff --git a/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue29962.cs b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue29962.cs new file mode 100644 index 000000000000..32ec192077d9 --- /dev/null +++ b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue29962.cs @@ -0,0 +1,23 @@ +using NUnit.Framework; +using UITest.Appium; +using UITest.Core; + +namespace Microsoft.Maui.TestCases.Tests.Issues; + +public class Issue29962 : _IssuesUITest +{ + public override string Issue => "[Windows] SearchBar PlaceHolder and Background Color should update properly at runtime"; + + public Issue29962(TestDevice device) + : base(device) + { } + + [Test] + [Category(UITestCategories.SearchBar)] + public void VerifySearchBarPlaceholderAndBackgroundColor() + { + App.WaitForElement("ColorChangeButton"); + App.Tap("ColorChangeButton"); + VerifyScreenshot(); + } +} \ No newline at end of file diff --git a/src/Controls/tests/TestCases.WinUI.Tests/snapshots/windows/VerifySearchBarPlaceholderAndBackgroundColor.png b/src/Controls/tests/TestCases.WinUI.Tests/snapshots/windows/VerifySearchBarPlaceholderAndBackgroundColor.png new file mode 100644 index 000000000000..bbe4bf4c4b39 Binary files /dev/null and b/src/Controls/tests/TestCases.WinUI.Tests/snapshots/windows/VerifySearchBarPlaceholderAndBackgroundColor.png differ diff --git a/src/Controls/tests/TestCases.iOS.Tests/snapshots/ios/VerifySearchBarPlaceholderAndBackgroundColor.png b/src/Controls/tests/TestCases.iOS.Tests/snapshots/ios/VerifySearchBarPlaceholderAndBackgroundColor.png new file mode 100644 index 000000000000..cec4e51485a5 Binary files /dev/null and b/src/Controls/tests/TestCases.iOS.Tests/snapshots/ios/VerifySearchBarPlaceholderAndBackgroundColor.png differ diff --git a/src/Core/src/Platform/Windows/SearchBarExtensions.cs b/src/Core/src/Platform/Windows/SearchBarExtensions.cs index 04067c2b00dd..305be09e3abd 100644 --- a/src/Core/src/Platform/Windows/SearchBarExtensions.cs +++ b/src/Core/src/Platform/Windows/SearchBarExtensions.cs @@ -7,7 +7,7 @@ namespace Microsoft.Maui.Platform { public static class SearchBarExtensions { - private static readonly string[] _backgroundColorKeys = + static readonly string[] _backgroundColorKeys = { "TextControlBackground", "TextControlBackgroundPointerOver", @@ -17,7 +17,7 @@ public static class SearchBarExtensions public static void UpdateBackground(this AutoSuggestBox platformControl, ISearchBar searchBar) { - UpdateColors(platformControl.Resources, _backgroundColorKeys, searchBar.Background?.ToPlatform()); + UpdateColors(platformControl, platformControl.Resources, _backgroundColorKeys, searchBar.Background?.ToPlatform()); } public static void UpdateIsEnabled(this AutoSuggestBox platformControl, ISearchBar searchBar) @@ -35,7 +35,7 @@ public static void UpdatePlaceholder(this AutoSuggestBox platformControl, ISearc platformControl.PlaceholderText = searchBar.Placeholder ?? string.Empty; } - private static readonly string[] _placeholderForegroundColorKeys = + static readonly string[] _placeholderForegroundColorKeys = { "TextControlPlaceholderForeground", "TextControlPlaceholderForegroundPointerOver", @@ -45,7 +45,7 @@ public static void UpdatePlaceholder(this AutoSuggestBox platformControl, ISearc public static void UpdatePlaceholderColor(this AutoSuggestBox platformControl, ISearchBar searchBar) { - UpdateColors(platformControl.Resources, _placeholderForegroundColorKeys, + UpdateColors(platformControl, platformControl.Resources, _placeholderForegroundColorKeys, searchBar.PlaceholderColor?.ToPlatform()); } @@ -54,7 +54,7 @@ public static void UpdateText(this AutoSuggestBox platformControl, ISearchBar se platformControl.Text = searchBar.Text; } - private static readonly string[] _foregroundColorKeys = + static readonly string[] _foregroundColorKeys = { "TextControlForeground", "TextControlForegroundPointerOver", @@ -66,7 +66,7 @@ public static void UpdateTextColor(this AutoSuggestBox platformControl, ISearchB { var tintBrush = searchBar.TextColor?.ToPlatform(); - if (tintBrush == null) + if (tintBrush is null) { platformControl.Resources.RemoveKeys(_foregroundColorKeys); platformControl.Foreground = null; @@ -80,7 +80,7 @@ public static void UpdateTextColor(this AutoSuggestBox platformControl, ISearchB platformControl.RefreshThemeResources(); } - private static void UpdateColors(ResourceDictionary resource, string[] keys, Brush? brush) + private static void UpdateColors(AutoSuggestBox platformControl, ResourceDictionary resource, string[] keys, Brush? brush) { if (brush is null) { @@ -90,6 +90,8 @@ private static void UpdateColors(ResourceDictionary resource, string[] keys, Bru { resource.SetValueForAllKey(keys, brush); } + + platformControl.RefreshThemeResources(); } public static void UpdateFont(this AutoSuggestBox platformControl, ISearchBar searchBar, IFontManager fontManager) => @@ -126,14 +128,20 @@ public static void UpdateMaxLength(this AutoSuggestBox platformControl, ISearchB } if (maxLength == 0) + { MauiAutoSuggestBox.SetIsReadOnly(platformControl, true); + } else + { MauiAutoSuggestBox.SetIsReadOnly(platformControl, searchBar.IsReadOnly); + } var currentControlText = platformControl.Text; if (currentControlText.Length > maxLength) + { platformControl.Text = currentControlText.Substring(0, maxLength); + } } public static void UpdateIsReadOnly(this AutoSuggestBox platformControl, ISearchBar searchBar) @@ -146,7 +154,9 @@ public static void UpdateIsTextPredictionEnabled(this AutoSuggestBox platformCon var textBox = platformControl.GetFirstDescendant(); if (textBox is null) + { return; + } textBox.UpdateIsTextPredictionEnabled(searchBar); } @@ -156,7 +166,9 @@ public static void UpdateIsSpellCheckEnabled(this AutoSuggestBox platformControl var textBox = platformControl.GetFirstDescendant(); if (textBox is null) + { return; + } textBox.UpdateIsSpellCheckEnabled(searchBar); } @@ -165,13 +177,15 @@ public static void UpdateKeyboard(this AutoSuggestBox platformControl, ISearchBa { var queryTextBox = platformControl.GetFirstDescendant(); - if (queryTextBox == null) + if (queryTextBox is null) + { return; + } queryTextBox.UpdateInputScope(searchBar); } - private static readonly string[] CancelButtonColorKeys = + static readonly string[] CancelButtonColorKeys = { "TextControlButtonForeground", "TextControlButtonForegroundPointerOver", @@ -183,7 +197,9 @@ internal static void UpdateCancelButtonColor(this AutoSuggestBox platformControl var cancelButton = platformControl.GetDescendantByName