Display search string in "TitleView" (Android)

валера карманов 396 Reputation points
2024-10-15T13:35:52.9133333+00:00

Using the "Shell.TitleView" element in the "AppShell" file, you can indeed change the top of the page. However, it works on all pages.

How to use the "TitleView" property of the "NavigationPage" class to change the contents of the "TitleView" element for specific pages and only when you click a button in C#.

Also, when you click a button (only a different one), return the contents of the element that was there.

Developer technologies | .NET | .NET MAUI
{count} votes

Answer accepted by question author
  1. Anonymous
    2024-10-16T06:09:22.55+00:00

    Hello,

    After displaying the "Entry" element, the search button and ellipsis are not needed there, how can I remove or hide them?

    please find searchItem.Clicked event, use ToolbarItems.Remove(searchItem); to hide them

    searchItem.Clicked += (s, e) => { 
    titleView = Shell.GetTitleView(this); 
    Shell.SetTitleView(this, new Entry() { BackgroundColor=Colors.Green, Margin = new Thickness(0, 7), PlaceholderColor = Color.FromRgba("#fff"), TextColor = Color.FromRgba("#fff"), FontSize = 16, Placeholder = "Search packages..." });
          //hide it at here
         ToolbarItems.Remove(searchItem);
         ToolbarItems.Remove(dropdownItem);
    };
    

    How to use the "TitleView" property of the "NavigationPage" class to change the contents of the "TitleView" element for specific pages and only when you click a button in C#.

    Also, when you click a button (only a different one), return the contents of the element that was there.

    If you want to add Searchbar in Titleview of the specific page. Please do not use Shell.SetTitleView(this, titleView); in AppShell.xaml.cs.

    For example, if you have a page called NewPage1, you just want to Searchbar in Titleview of this page. You can set it in the background code like following code. You do not need to use Entry and a Icon to create a searchview, just add SearchBar directly when you click the search icon, SearchButtonPressed event will be executed.

    public partial class NewPage1 : ContentPage
    {
    	public NewPage1()
    	{
    		InitializeComponent();
     
            SearchBar _searchBar1 = new SearchBar();
            _searchBar1.SearchButtonPressed += _searchBar1_SearchButtonPressed;
            Shell.SetTitleView(this, _searchBar1);
     
        }
     
        private void _searchBar1_SearchButtonPressed(object? sender, EventArgs e)
        {
           //set the search content here
        }
     
    }
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    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.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.