Skip to content

Commit

Permalink
[iOS/MacCatalyst] Fix SearchBar TextChanged event not working issue (#…
Browse files Browse the repository at this point in the history
…5462)

* Fix SearchBar TextChanged event not working on iOS/Catalyst

* Removed SetupDefaults

Co-authored-by: Rui Marinho <[email protected]>
  • Loading branch information
jsuarezruiz and rmarinho authored Mar 30, 2022
1 parent 2288eea commit 43547cb
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@
Style="{StaticResource Headline}"/>
<SearchBar
IsTextPredictionEnabled="False"/>
<Label
Text="TextChanged"
Style="{StaticResource Headline}"/>
<SearchBar
TextChanged="OnSearchBarTextChanged"/>
<Label
Text="Background color"
Style="{StaticResource Headline}"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
namespace Maui.Controls.Sample.Pages
using System.Diagnostics;
using Microsoft.Maui.Controls;

namespace Maui.Controls.Sample.Pages
{
public partial class SearchBarPage
{
public SearchBarPage()
{
InitializeComponent();
}

void OnSearchBarTextChanged(object sender, TextChangedEventArgs args)
{
var text = ((SearchBar)sender).Text;
Debug.WriteLine($"SearchBar Text changed: {text}");
}
}
}
15 changes: 14 additions & 1 deletion src/Core/src/Handlers/SearchBar/SearchBarHandler.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ protected override void ConnectHandler(MauiSearchBar platformView)

platformView.OnEditingStarted += OnEditingStarted;
platformView.OnEditingStopped += OnEditingEnded;


if (_editor != null)
_editor.EditingChanged += OnEditingChanged;

base.ConnectHandler(platformView);
}

Expand All @@ -45,6 +48,8 @@ protected override void DisconnectHandler(MauiSearchBar platformView)
platformView.OnEditingStarted -= OnEditingStarted;
platformView.OnEditingStopped -= OnEditingEnded;

if (_editor != null)
_editor.EditingChanged -= OnEditingChanged;

base.DisconnectHandler(platformView);
}
Expand Down Expand Up @@ -171,5 +176,13 @@ void OnEditingStarted(object? sender, EventArgs e)
if (VirtualView != null)
VirtualView.IsFocused = true;
}

void OnEditingChanged(object? sender, EventArgs e)
{
if (VirtualView == null || _editor == null)
return;

VirtualView.UpdateText(_editor.Text);
}
}
}

0 comments on commit 43547cb

Please sign in to comment.