.NET MAUI SearchBar Control
|
|
.NET MAUI- SearchBar
In .NET MAUI, the SearchBar is a specialized user input control designed specifically for initiating and managing search queries.
Unlike a standard single-line Entry control, it provides a native search interface that includes built-in elements like a search icon and a cancellation ("X") button.
๐ What is a SearchBar?
A SearchBar is a text input control optimized for search operations. It provides a consistent UI pattern across platforms for entering queries and triggering search actions.
๐ Key Features
- Text Input: Accepts single-line text, similar to Entry.
- Placeholder: Displays hint text when empty (e.g., โSearchโฆโ).
- Search Button: Includes a platform-specific search icon/button to trigger search.
-
Events:
- SearchButtonPressed โ Fired when the user taps the search button.
- TextChanged โ Fired when the text changes.
- Styling: Supports font, color, alignment, and background customization.
- Data Binding: Bind Text property to ViewModel for MVVM patterns.
- Keyboard Optimization: Shows a search-specific keyboard (with a search button) on mobile devices.
๐ Example Code
Basic SearchBar
<SearchBar Placeholder="Search items"
Text="{Binding Query}"
SearchButtonPressed="OnSearch"/>
Handling Search in Code-Behind
void OnSearch(object sender, EventArgs e)
{
var searchBar = (SearchBar)sender;
DisplayAlert("Search", $"You searched for: {searchBar.Text}", "OK");
}
โ
Why Choose SearchBar?
- Optimized for Search: Provides a familiar UI pattern for users.
- Built-in Search Button: No need to add extra buttons for triggering search.
- MVVM Friendly: Easily bind search queries to your ViewModel.
- Cross-Platform Consistency: Same search experience across Android, iOS, macOS, and Windows.
๐ Common Use Cases
- App Search: Searching items in a list or database.
- Filtering: Narrow down results in a CollectionView or ListView.
- Navigation: Search for destinations in a map app.
- Content Discovery: Search articles, products, or media.
โ ๏ธ Considerations
- Single-Line Only: Like Entry, it doesnโt support multi-line input.
- Requires Logic: You must handle search events (SearchButtonPressed) to perform filtering or queries.
- Performance: For large datasets, combine with CollectionView and efficient filtering.
โจ In short:
The MAUI SearchBar is a specialized input control for search queries, offering a familiar UI, built-in search button, and easy binding. Itโs perfect for filtering lists, searching content, or navigating data-heavy apps.
Key Features and Properties
- Search Integration: Features properties like SearchCommand and SearchCommandParameter, allowing you to bind search logic directly to a ViewModel.
-
Visual Customization:
- SearchIconColor: Controls the color of the magnifying glass icon.
- CancelButtonColor: Sets the color of the button used to clear the search query.
- Placeholder: Displays hint text (e.g., "Search...") when the bar is empty.
- Text Handling: Supports HorizontalTextAlignment, VerticalTextAlignment, and CharacterSpacing for precise text positioning.
- ReturnType: Specifies the appearance of the "Enter" key on the soft keyboard, with the default usually set to a "Search" button.
Essential Events
- SearchButtonPressed: Triggered when the user clicks the search icon or presses the enter key on their keyboard.
- TextChanged: Fired on every keystroke, enabling real-time filtering as the user types.
2026 Platform Updates (.NET 10)
As of .NET 10 (released late 2025), the SearchBar has received several refinements:
- Enhanced Styling: New bindable properties like SearchIconColor and ReturnType are now fully supported to offer more granular control over the search experience.
- Improved Keyboard Control: New methods within the SoftInputExtensions class allow developers to programmatically show or hide the soft keyboard more reliably across platforms.
- Performance: Recent framework updates focus on faster runtime rendering and improved debug experiences, particularly on Android and iOS.
For complex search requirements like auto-complete or suggestions, developers often use the SearchHandler within MAUI Shell or third-party autocomplete controls.