Adding Search Icon to Toolbar

Bubba Jones 206 Reputation points
2021-05-04T09:29:08.3+00:00

93525-code.txt

I have been trying to insert a search control in a toolbar. I understand that in order to do this I need to write a custom renderer. I have based myself on the following:

https://codetraveler.io/2019/10/05/adding-a-search-bar-to-xamarin-forms-navigationpage/

With the complete solution found here:

https://github.com/brminnick/GitTrends

I have integrated the relevant portions of the above solution into my project. However despite my best efforts have not been able to get the search icon to appear in my toolbar. My code is displayed below. When I debug the SearchPageRenderer.cs in the android project, OnElementChanged gets called which then calls AddSearchToToolbar. This behavior is correct. In AddSearchToToolbar all the if statements in it check out which is also correct. Still no search icon appears in the toolbar. Its been a week and i'm all out of ideas at this point.

If there are other more simple solutions to implementing this, i'm all ears. This has been my first (and very unpleasent) experience with custom renderers.

My relevant code is viewable in the attachment.

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,253 questions
0 comments No comments
{count} votes

Accepted answer
  1. JarvanZhang 23,931 Reputation points
    2021-05-04T11:14:01.373+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    If there are other more simple solutions to implementing this

    To display a view in Navigation Bar, you could just use the NavigationPage.TitleView.

       <ContentPage ...>  
           <NavigationPage.TitleView>  
               <SearchBar />  
           </NavigationPage.TitleView>  
             
           <ContentPage.Content>  
               ...  
           </ContentPage.Content>  
       </ContentPage>  
    

    Check the doc: https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/navigation/hierarchical#displaying-views-in-the-navigation-bar

    Best Regards,

    Jarvan Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful