Forum Xamarin.iOS
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

SearchButtonClicked never called on ios

EsegeEsege Member ✭✭
edited October 9 in Xamarin.iOS

I made a custom renderer for Xamarin.Forms SearchBar.

[assembly: ExportRenderer(typeof(SearchBar), typeof(SearchBarRendererForiOS))]
namespace SearchBarSample.iOS
{
    public class SearchBarRendererForiOS : SearchBarRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.SearchBar> e)
        {
            base.OnElementChanged(e);

           Control.ShowsBookmarkButton = true;
           Control.SearchButtonClicked += ControlOnSearchButtonClicked;
           Control.BookmarkButtonClicked += ControlOnBookmarkButtonClicked;
        }

        private void ControlOnBookmarkButtonClicked(object sender, EventArgs e)
        {
            ...
        }

        private void ControlOnSearchButtonClicked(object sender, EventArgs e)
        {
           ...
        }
    }
}

BookmarkButtonClicked event works just fine. But SearchButtonClicked event doesn't fire when I click to search button. What am I doing wrong?

I reproduced the issue. Download the project from here.

Best Answer

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    It is so weird that if I create a new custom renderer in your sample with the same code, all of the events work.
    However, it seems this is only a breakpoint issue.
    If I added a messaging in the MainPage:

    public MainPage()
    {
        InitializeComponent();
    
        MessagingCenter.Subscribe<object, string>(this, "Test", (sender, msg) =>
        {
            DisplayAlert("", msg, "OK");
        });
    }
    

    Send messages separately in the renderer:

    private void ControlOnSearchButtonClicked(object sender, EventArgs e)
    {
        //doesnt work
        MessagingCenter.Send<object, string>(this, "Test", "search clicked");
    }
    
    private void ControlOnBookmarkButtonClicked(object sender, EventArgs e)
    {
        //works
        MessagingCenter.Send<object, string>(this, "Test", "bookmark clicked");
    }
    

    The breakpoints weren't fired but dialog presents:

    This means the code there does run properly.

  • EsegeEsege Member ✭✭

    @LandLu said:
    It is so weird that if I create a new custom renderer in your sample with the same code, all of the events work.
    However, it seems this is only a breakpoint issue.
    If I added a messaging in the MainPage:

    public MainPage()
    {
        InitializeComponent();
    
        MessagingCenter.Subscribe<object, string>(this, "Test", (sender, msg) =>
        {
            DisplayAlert("", msg, "OK");
        });
    }
    

    Send messages separately in the renderer:

    private void ControlOnSearchButtonClicked(object sender, EventArgs e)
    {
        //doesnt work
        MessagingCenter.Send<object, string>(this, "Test", "search clicked");
    }
    
    private void ControlOnBookmarkButtonClicked(object sender, EventArgs e)
    {
        //works
        MessagingCenter.Send<object, string>(this, "Test", "bookmark clicked");
    }
    

    The breakpoints weren't fired but dialog presents:

    This means the code there does run properly.

    Thanks for the answer and I'm sorry for replying so late. I can't display the keyboard on my ios simulator but from what I understand by examining the gif SearchButtonClicked doesn't fire when tapped on the search icon. It fires when tapped on the search button on the keyboard? Is that correct? I need to detect the search icon click. It works fine on UWP and Android(with a renderer ).

    [assembly: ExportRenderer(typeof(MySearchBar), typeof(MySearchBarRenderer))]
    
    
    namespace itfaiye.geoerp.xamarin.Droid.Renderers
    {
        public class MySearchBarRenderer: SearchBarRenderer
        {
            public MySearchBarRenderer(Context context) : base(context)
            {
            }
            protected override void OnElementChanged(ElementChangedEventArgs<SearchBar> e)
            {
                base.OnElementChanged(e);
    
                if (Control != null)
                {
                    var searchView = Control;
    
                    int searchViewCloseButtonId = Control.Resources.GetIdentifier("android:id/search_mag_icon", null, null);
                    var closeIcon = searchView.FindViewById(searchViewCloseButtonId);
    
                    closeIcon.Click += CloseIconOnClick;
                }
            }
    
            private void CloseIconOnClick(object sender, EventArgs e)
            {
                Element.OnSearchButtonPressed();
            }
        }
    }
    
  • LandLuLandLu Member, Xamarin Team Xamurai

    It fires when tapped on the search button on the keyboard?

    Yes.
    Keyboard sometimes dismisses on the simulators. Try to show it on Mac using command + k.
    This also helps:

  • EsegeEsege Member ✭✭

    @LandLu said:

    It fires when tapped on the search button on the keyboard?

    Yes.
    Keyboard sometimes dismisses on the simulators. Try to show it on Mac using command + k.
    This also helps:

    Thank you but is it not possible to detect the real search icon click on ios. As I said it works fine on UWP, android(with a renderer). Is there any workaround?

  • EsegeEsege Member ✭✭

    @LandLu said:
    Do you mean the icon aligned on the left side?
    iOS is different.
    It won't trigger the search event. We could only complete the searching by clicking the button on the keyboard.

    Yes. I guess that's it then. Thank you.

Sign In or Register to comment.