Xamarin.Forms Guides | Samples

Xamarin.Forms.SearchBar Class

A View control that provides a search box.

See Also: SearchBar

Syntax

[Xamarin.Forms.RenderWith(typeof(Xamarin.Forms.Platform._SearchBarRenderer))]
public class SearchBar : View, IElementConfiguration<SearchBar>, ISearchBarController

Remarks

The following example shows a basic use.

C# Example

public class App : Application
{
    Label resultsLabel;
    SearchBar searchBar;
    public App ()
    {
        resultsLabel = new Label {
            Text = "Result will appear here.",
            VerticalOptions = LayoutOptions.FillAndExpand,
            FontSize = 25
        };

        searchBar = new SearchBar {
            Placeholder = "Enter search term",
            SearchCommand = new Command(() => {resultsLabel.Text = "Result: " + searchBar.Text + " is what you asked for.";})
        };
                
        MainPage = new ContentPage {
            Content = new StackLayout {
                VerticalOptions = LayoutOptions.Start,
                Children = {
                    new Label {
                        HorizontalTextAlignment = TextAlignment.Center,
                        Text = "SearchBar",
                        FontSize = 50
                    },
                    searchBar,
                    new ScrollView
                    {
                        Content = resultsLabel,
                        VerticalOptions = LayoutOptions.FillAndExpand
                    }
                },
                Padding = new Thickness(10, Device.OnPlatform(20, 0, 0), 10, 5)
            }
        };
    }
    
    // OnStart, OnSleep, and OnResume implementations, & etc.
    
}

XAML for Xamarin.Forms supports the following properties for the SearchBar class:

PropertyValue
CancelButtonColor

A color specification, with or without the prefix, "Color". For example, "Color.Red" and "Red" both specify the color red.

Placeholder

The default text that will appear in the search bar when it is empty.

SearchCommand

XAML markup extension that describes a binding to a command.

SearchCommandParameter

The parameter for the search command.

Text

The initial text that will appear in the search bar.

Requirements

Namespace: Xamarin.Forms
Assembly: Xamarin.Forms.Core (in Xamarin.Forms.Core.dll)
Assembly Versions: 1.0.0.0, 1.1.0.0, 1.2.0.0, 1.3.0.0, 1.4.0.0, 1.5.0.0, 2.0.0.0

The members of Xamarin.Forms.SearchBar are listed below.

See Also: View

Public Constructors

Creates a new SearchBar.

Public Fields

static readonly
CancelButtonColorPropertyBindableProperty. Backing store for the CancelButtonColor property.
static readonly
FontAttributesPropertyBindableProperty. Backing store for the FontAttributes property.
static readonly
FontFamilyPropertyBindableProperty. Backing store for the FontFamily property.
static readonly
FontSizePropertyBindableProperty. Backing store for the FontSize property.
static readonly
HorizontalTextAlignmentPropertyBindableProperty. Backing store for the SearchBar.HorizontalTextAlignment property.
static readonly
PlaceholderColorPropertyBindableProperty. Backing store for the SearchBar.PlaceholderColor property.
static readonly
PlaceholderPropertyBindableProperty. Backing store for the SearchBar.Placeholder property.
static readonly
SearchCommandParameterPropertyBindableProperty. Backing store for the SearchBar.SearchCommandParameter property.
static readonly
SearchCommandPropertyBindableProperty. Backing store for the SearchBar.SearchCommand property.
static readonly
TextColorPropertyBindableProperty. Backing store for the SearchBar.TextColor property.
static readonly
TextPropertyBindableProperty. Backing store for the SearchBar.Text property.

Public Properties

CancelButtonColorColor. Gets or sets the color of the cancel button.
FontAttributesFontAttributes. Gets a value that indicates whether the font for the searchbar text is bold, italic, or neither.
FontFamilyString. Gets or sets the font family for the search bar text.
FontSizeDouble. Gets the size of the font for the text in the searchbar.
HorizontalTextAlignmentTextAlignment. Gets or sets the horizontal text alignment.
PlaceholderString. Gets or sets the text that is displayed when the SearchBar is empty.
PlaceholderColorColor. Gets or sets the color of the placheholder text.
SearchCommandICommand. Gets or sets the command that is run when the user presses Search button.
SearchCommandParameterObject. Gets or sets the parameter that is sent to the SearchBar.SearchCommand.
TextString. Gets or sets the text that is displayed in the SearchBar.
TextColorColor. Gets or sets the text color.

Public Methods

On<T>() : IPlatformElementConfiguration<T, SearchBar>
Returns the platform-specific instance of this SearchBar, on which a platform-specific method may be called.

Public Events

SearchButtonPressedEvent that is raised when the user presses the Search button.
TextChangedEvent that is raised when the SearchBar.Text property is changed. For example, this event is raised as the user edits the text in the SearchBar.

Explicitly Implemented Interface Members