Skip to content

Commit fc59094

Browse files
[Windows] Fixed runtime update issue for SearchBar PlaceholderColor and BackgroundColor (#29965)
* Fixed SearchBar color issues * Added test sample * Added Windows and Mac snapshots
1 parent 7fe933c commit fc59094

File tree

7 files changed

+86
-9
lines changed

7 files changed

+86
-9
lines changed
25.9 KB
Loading
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
namespace Maui.Controls.Sample.Issues;
2+
3+
[Issue(IssueTracker.Github, 29962, "[Windows] SearchBar PlaceHolder and Background Color should update properly at runtime", PlatformAffected.UWP)]
4+
public class Issue29962 : ContentPage
5+
{
6+
public Issue29962()
7+
{
8+
var searchBar = new SearchBar
9+
{
10+
Placeholder = "Search here...",
11+
Margin = new Thickness(0, 30),
12+
BackgroundColor = Colors.Black,
13+
PlaceholderColor = Colors.Yellow,
14+
HeightRequest = 50,
15+
};
16+
var button = new Button
17+
{
18+
Text = "Change PlaceHolder and Background Color",
19+
VerticalOptions = LayoutOptions.Center,
20+
HorizontalOptions = LayoutOptions.Center,
21+
AutomationId = "ColorChangeButton"
22+
};
23+
button.Clicked += (sender, e) =>
24+
{
25+
searchBar.BackgroundColor = Colors.YellowGreen;
26+
searchBar.PlaceholderColor = Colors.Red;
27+
};
28+
29+
Content = new StackLayout
30+
{
31+
Children =
32+
{
33+
searchBar,
34+
button
35+
}
36+
};
37+
}
38+
}
12.1 KB
Loading
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using NUnit.Framework;
2+
using UITest.Appium;
3+
using UITest.Core;
4+
5+
namespace Microsoft.Maui.TestCases.Tests.Issues;
6+
7+
public class Issue29962 : _IssuesUITest
8+
{
9+
public override string Issue => "[Windows] SearchBar PlaceHolder and Background Color should update properly at runtime";
10+
11+
public Issue29962(TestDevice device)
12+
: base(device)
13+
{ }
14+
15+
[Test]
16+
[Category(UITestCategories.SearchBar)]
17+
public void VerifySearchBarPlaceholderAndBackgroundColor()
18+
{
19+
App.WaitForElement("ColorChangeButton");
20+
App.Tap("ColorChangeButton");
21+
VerifyScreenshot();
22+
}
23+
}
8.4 KB
Loading
28.9 KB
Loading

src/Core/src/Platform/Windows/SearchBarExtensions.cs

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace Microsoft.Maui.Platform
77
{
88
public static class SearchBarExtensions
99
{
10-
private static readonly string[] _backgroundColorKeys =
10+
static readonly string[] _backgroundColorKeys =
1111
{
1212
"TextControlBackground",
1313
"TextControlBackgroundPointerOver",
@@ -17,7 +17,7 @@ public static class SearchBarExtensions
1717

1818
public static void UpdateBackground(this AutoSuggestBox platformControl, ISearchBar searchBar)
1919
{
20-
UpdateColors(platformControl.Resources, _backgroundColorKeys, searchBar.Background?.ToPlatform());
20+
UpdateColors(platformControl, platformControl.Resources, _backgroundColorKeys, searchBar.Background?.ToPlatform());
2121
}
2222

2323
public static void UpdateIsEnabled(this AutoSuggestBox platformControl, ISearchBar searchBar)
@@ -35,7 +35,7 @@ public static void UpdatePlaceholder(this AutoSuggestBox platformControl, ISearc
3535
platformControl.PlaceholderText = searchBar.Placeholder ?? string.Empty;
3636
}
3737

38-
private static readonly string[] _placeholderForegroundColorKeys =
38+
static readonly string[] _placeholderForegroundColorKeys =
3939
{
4040
"TextControlPlaceholderForeground",
4141
"TextControlPlaceholderForegroundPointerOver",
@@ -45,7 +45,7 @@ public static void UpdatePlaceholder(this AutoSuggestBox platformControl, ISearc
4545

4646
public static void UpdatePlaceholderColor(this AutoSuggestBox platformControl, ISearchBar searchBar)
4747
{
48-
UpdateColors(platformControl.Resources, _placeholderForegroundColorKeys,
48+
UpdateColors(platformControl, platformControl.Resources, _placeholderForegroundColorKeys,
4949
searchBar.PlaceholderColor?.ToPlatform());
5050
}
5151

@@ -54,7 +54,7 @@ public static void UpdateText(this AutoSuggestBox platformControl, ISearchBar se
5454
platformControl.Text = searchBar.Text;
5555
}
5656

57-
private static readonly string[] _foregroundColorKeys =
57+
static readonly string[] _foregroundColorKeys =
5858
{
5959
"TextControlForeground",
6060
"TextControlForegroundPointerOver",
@@ -66,7 +66,7 @@ public static void UpdateTextColor(this AutoSuggestBox platformControl, ISearchB
6666
{
6767
var tintBrush = searchBar.TextColor?.ToPlatform();
6868

69-
if (tintBrush == null)
69+
if (tintBrush is null)
7070
{
7171
platformControl.Resources.RemoveKeys(_foregroundColorKeys);
7272
platformControl.Foreground = null;
@@ -80,7 +80,7 @@ public static void UpdateTextColor(this AutoSuggestBox platformControl, ISearchB
8080
platformControl.RefreshThemeResources();
8181
}
8282

83-
private static void UpdateColors(ResourceDictionary resource, string[] keys, Brush? brush)
83+
private static void UpdateColors(AutoSuggestBox platformControl, ResourceDictionary resource, string[] keys, Brush? brush)
8484
{
8585
if (brush is null)
8686
{
@@ -90,6 +90,8 @@ private static void UpdateColors(ResourceDictionary resource, string[] keys, Bru
9090
{
9191
resource.SetValueForAllKey(keys, brush);
9292
}
93+
94+
platformControl.RefreshThemeResources();
9395
}
9496

9597
public static void UpdateFont(this AutoSuggestBox platformControl, ISearchBar searchBar, IFontManager fontManager) =>
@@ -126,14 +128,20 @@ public static void UpdateMaxLength(this AutoSuggestBox platformControl, ISearchB
126128
}
127129

128130
if (maxLength == 0)
131+
{
129132
MauiAutoSuggestBox.SetIsReadOnly(platformControl, true);
133+
}
130134
else
135+
{
131136
MauiAutoSuggestBox.SetIsReadOnly(platformControl, searchBar.IsReadOnly);
137+
}
132138

133139
var currentControlText = platformControl.Text;
134140

135141
if (currentControlText.Length > maxLength)
142+
{
136143
platformControl.Text = currentControlText.Substring(0, maxLength);
144+
}
137145
}
138146

139147
public static void UpdateIsReadOnly(this AutoSuggestBox platformControl, ISearchBar searchBar)
@@ -146,7 +154,9 @@ public static void UpdateIsTextPredictionEnabled(this AutoSuggestBox platformCon
146154
var textBox = platformControl.GetFirstDescendant<TextBox>();
147155

148156
if (textBox is null)
157+
{
149158
return;
159+
}
150160

151161
textBox.UpdateIsTextPredictionEnabled(searchBar);
152162
}
@@ -156,7 +166,9 @@ public static void UpdateIsSpellCheckEnabled(this AutoSuggestBox platformControl
156166
var textBox = platformControl.GetFirstDescendant<TextBox>();
157167

158168
if (textBox is null)
169+
{
159170
return;
171+
}
160172

161173
textBox.UpdateIsSpellCheckEnabled(searchBar);
162174
}
@@ -165,13 +177,15 @@ public static void UpdateKeyboard(this AutoSuggestBox platformControl, ISearchBa
165177
{
166178
var queryTextBox = platformControl.GetFirstDescendant<TextBox>();
167179

168-
if (queryTextBox == null)
180+
if (queryTextBox is null)
181+
{
169182
return;
183+
}
170184

171185
queryTextBox.UpdateInputScope(searchBar);
172186
}
173187

174-
private static readonly string[] CancelButtonColorKeys =
188+
static readonly string[] CancelButtonColorKeys =
175189
{
176190
"TextControlButtonForeground",
177191
"TextControlButtonForegroundPointerOver",
@@ -183,7 +197,9 @@ internal static void UpdateCancelButtonColor(this AutoSuggestBox platformControl
183197
var cancelButton = platformControl.GetDescendantByName<Button>("DeleteButton");
184198

185199
if (cancelButton is null)
200+
{
186201
return;
202+
}
187203

188204
cancelButton.UpdateTextColor(searchBar.CancelButtonColor, CancelButtonColorKeys);
189205
}

0 commit comments

Comments
 (0)