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.