Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[iOS/MacCatalyst] Fix SearchBar TextChanged event not working issue #5462

Merged
merged 4 commits into from
Mar 30, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Expand Up @@ -74,6 +74,11 @@
Style="{StaticResource Headline}"/>
<SearchBar
IsTextPredictionEnabled="False"/>
<Label
Text="TextChanged"
Style="{StaticResource Headline}"/>
<SearchBar
TextChanged="OnSearchBarTextChanged"/>
</VerticalStackLayout>
</ScrollView>
</views:BasePage.Content>
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}");
}
}
}
16 changes: 15 additions & 1 deletion src/Core/src/Handlers/SearchBar/SearchBarHandler.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,13 @@ protected override void ConnectHandler(MauiSearchBar platformView)

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

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

SetupDefaults(platformView);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you remove the SetupDefaults from this? if you merge with main that might delete it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@PureWeen Updated, could you review it again?. Thanks!.


base.ConnectHandler(platformView);
SetupDefaults(platformView);
}

protected override void DisconnectHandler(MauiSearchBar platformView)
Expand All @@ -52,6 +56,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 @@ -190,5 +196,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);
}
}
}