question

ShantimohanElchuri-8757 avatar image
0 Votes"
ShantimohanElchuri-8757 asked RobCaplan edited

Tapping "Cancel" of a SearchBar is crashing the app in Xamarin.iOS project

I have a search bar in my app. Along with x a clickable button named "Cancel" appears on iOS. No issues clicking the x to clear the text. But when I clicked the "Cancel" button the app is crashing. This "Cancel" buttone doesn't appear in Android. And there is no event name like "CancelClicked" for SearchBar.

dotnet-xamarin
· 6
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

The Cancle button only appears in iOS and it doesn't appear in Android, it caused by the differences of these two platforms, the native SearchBar displays differently.
In addition, could you provide the crash information or share a demo to test?


0 Votes 0 ·

I could view the logs on my Mac but not able to save it to a file to attach here. No option is provided. No context menu appearing on right clicking the log view.

0 Votes 0 ·

I created a dummy project and the issue wasn't there. So it is in my production project only.

I tried to put SearchBar within a Frame or StackLayout but it was creating NullReferenceException. Also after adding the SearchBar above a ListView, initial populating the ListView has become horribly slow. I don't know if these two are related.

ThanQ...

0 Votes 0 ·
WenyanZhang-MSFT avatar image WenyanZhang-MSFT ShantimohanElchuri-8757 ·

You could try to take a screenshot about logs or select all to copy. If your new project can't reproduce this issue, you could check the difference between them. NullReferenceException means that you access a member on a type whose value is null.

0 Votes 0 ·
Show more comments

1 Answer

ShantimohanElchuri-8757 avatar image
0 Votes"
ShantimohanElchuri-8757 answered

@WenyanZhang-MSFT I got the solution. Fortunately when I was debugging on a device, tapping the Cancel button broke in SearchBar_TextChanged event pointing to what element is null. It is none other than the SearchBar.Text. So testing for null before using it resolved the issue. The code is as below:

 private void sbSearchData_TextChanged(object sender, TextChangedEventArgs e)
 {
     SearchBar sb = (SearchBar)sender;

     // This test is needed to avoid crashing of iOS app on
     //    tapping the Cancel button that appears when something
     //    is entered into the SearchBar's text box.
     if (!string.IsNullOrEmpty(sb.Text))
     {
         itemsVM.searchFilter = sb.Text.Trim().ToLower();
         itemsVM.LoadItemsCommand.Execute(null);
     }


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.