diff --git a/src/Core/src/Handlers/SearchBar/SearchBarHandler.iOS.cs b/src/Core/src/Handlers/SearchBar/SearchBarHandler.iOS.cs index 3d2d79115ef9..9aef1221aa2b 100644 --- a/src/Core/src/Handlers/SearchBar/SearchBarHandler.iOS.cs +++ b/src/Core/src/Handlers/SearchBar/SearchBarHandler.iOS.cs @@ -159,6 +159,8 @@ class MauiSearchBarProxy ISearchBar? VirtualView => _virtualView is not null && _virtualView.TryGetTarget(out var v) ? v : null; + SearchBarHandler? Handler => _handler is not null && _handler.TryGetTarget(out var h) ? h : null; + public void Connect(SearchBarHandler handler, ISearchBar virtualView, MauiSearchBar platformView) { _handler = new(handler); @@ -179,6 +181,7 @@ public void Connect(SearchBarHandler handler, ISearchBar virtualView, MauiSearch public void Disconnect(MauiSearchBar platformView, UITextField? editor) { _virtualView = null; + _handler = null; platformView.CancelButtonClicked -= OnCancelClicked; platformView.SearchButtonClicked -= OnSearchButtonClicked; @@ -196,7 +199,7 @@ void OnMovedToWindow(object? sender, EventArgs e) { // The cancel button doesn't exist until the control has moved to the window // so we fire this off again so it can set the color - if (_handler is not null && _handler.TryGetTarget(out var handler)) + if (Handler is SearchBarHandler handler) { handler.UpdateValue(nameof(ISearchBar.CancelButtonColor)); } @@ -219,7 +222,7 @@ void OnTextPropertySet(object? sender, UISearchBarTextChangedEventArgs a) { virtualView.UpdateText(a.SearchText); - if (_handler is not null && _handler.TryGetTarget(out var handler)) + if (Handler is SearchBarHandler handler) { handler.UpdateCancelButtonVisibility(); } @@ -240,7 +243,7 @@ void OnEditingStarted(object? sender, EventArgs e) void OnEditingChanged(object? sender, EventArgs e) { - if (_handler is not null && _handler.TryGetTarget(out var handler)) + if (Handler is SearchBarHandler handler) { handler.UpdateCancelButtonVisibility(); }