Skip to content

Commit 7bcea52

Browse files
authored
[Windows] Fixed the issue with SearchBar focus and unfocus events. (#28529)
* Fixed the Searchbar Focus and Unfocus events issue * Utilized the existing method in ViewHandler.Windows
1 parent 42e2dc1 commit 7bcea52

File tree

4 files changed

+69
-1
lines changed

4 files changed

+69
-1
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
namespace Maui.Controls.Sample.Issues;
2+
3+
[Issue(IssueTracker.Github, 28419, "SearchBar focus/unfocus do not fire on Windows", PlatformAffected.UWP)]
4+
public class Issue28419 : ContentPage
5+
{
6+
public Issue28419()
7+
{
8+
var label = new Label();
9+
var searchBar = new SearchBar { Placeholder = "SearchBar", AutomationId = "SearchBar" };
10+
var entry = new Entry { Placeholder = "Entry", AutomationId = "Entry" };
11+
12+
searchBar.Focused += (sender, e) =>
13+
{
14+
label.Text = "SearchBar Focused";
15+
};
16+
17+
searchBar.Unfocused += (sender, e) =>
18+
{
19+
label.Text = "SearchBar Unfocused";
20+
};
21+
22+
Content = new VerticalStackLayout
23+
{
24+
Spacing = 10,
25+
Children = { label, searchBar, entry }
26+
};
27+
}
28+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using NUnit.Framework;
2+
using UITest.Appium;
3+
using UITest.Core;
4+
5+
namespace Microsoft.Maui.TestCases.Tests.Issues;
6+
public class Issue28419 : _IssuesUITest
7+
{
8+
public Issue28419(TestDevice device) : base(device) { }
9+
10+
public override string Issue => "SearchBar focus/unfocus do not fire on Windows";
11+
12+
[Test]
13+
[Category(UITestCategories.SearchBar)]
14+
public void SearchBarShouldTriggerFocusedAndUnFocusedEvents()
15+
{
16+
App.WaitForElement("SearchBar");
17+
App.Tap("SearchBar");
18+
App.WaitForElement("SearchBar Focused");
19+
App.Tap("Entry");
20+
App.WaitForElement("SearchBar Unfocused");
21+
}
22+
}

src/Core/src/Handlers/SearchBar/SearchBarHandler.Windows.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,21 @@ protected override void ConnectHandler(AutoSuggestBox platformView)
1818
platformView.Loaded += OnLoaded;
1919
platformView.QuerySubmitted += OnQuerySubmitted;
2020
platformView.TextChanged += OnTextChanged;
21+
//In ViewHandler.Windows, FocusManager.GotFocus and LostFocus are handled for other controls.
22+
// However, for AutoSuggestBox, when handling the GotFocus or LostFocus methods, tapping the AutoSuggestBox causes e.NewFocusedElement and e.OldFocusedElement to be a TextBox (which receives the focus).
23+
// As a result, when comparing the PlatformView with the appropriate handler in FocusManagerMapping, the condition is not satisfied, causing the focus and unfocus methods to not work correctly.
24+
// To address this, I have specifically handled the focus and unfocus events for AutoSuggestBox here.
25+
platformView.GotFocus += OnGotFocus;
26+
platformView.LostFocus += OnLostFocus;
2127
}
2228

2329
protected override void DisconnectHandler(AutoSuggestBox platformView)
2430
{
2531
platformView.Loaded -= OnLoaded;
2632
platformView.QuerySubmitted -= OnQuerySubmitted;
2733
platformView.TextChanged -= OnTextChanged;
34+
platformView.GotFocus -= OnGotFocus;
35+
platformView.LostFocus -= OnLostFocus;
2836
}
2937

3038
public static void MapBackground(ISearchBarHandler handler, ISearchBar searchBar)
@@ -148,5 +156,15 @@ void OnTextChanged(AutoSuggestBox? sender, AutoSuggestBoxTextChangedEventArgs e)
148156

149157
VirtualView.Text = sender.Text;
150158
}
159+
160+
void OnGotFocus(object sender, UI.Xaml.RoutedEventArgs e)
161+
{
162+
UpdateIsFocused(true);
163+
}
164+
165+
void OnLostFocus(object sender, UI.Xaml.RoutedEventArgs e)
166+
{
167+
UpdateIsFocused(false);
168+
}
151169
}
152170
}

src/Core/src/Handlers/View/ViewHandler.Windows.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ static void FocusManager_LostFocus(object? sender, FocusManagerLostFocusEventArg
155155
}
156156
}
157157

158-
void UpdateIsFocused(bool isFocused)
158+
private protected void UpdateIsFocused(bool isFocused)
159159
{
160160
if (VirtualView is not { } virtualView)
161161
{

0 commit comments

Comments
 (0)