简体   繁体   中英

xamarin forms - clear SearchBar

<NavigationPage.TitleView>
                <StackLayout BackgroundColor="Red" HorizontalOptions="FillAndExpand" VerticalOptions="StartAndExpand" Orientation="Horizontal">
                    <SearchBar x:Name="SearchBar" BackgroundColor="#Brown" TextChanged="SearchBar_TextChanged" HorizontalOptions="FillAndExpand" Placeholder="Search..." PlaceholderColor="Gray" TextColor="White" VerticalOptions="StartAndExpand"/>
                </StackLayout>
            </NavigationPage.TitleView> 

Why is there no event to clear 'SearchBar'

https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/searchbar

The scenario being the user types a product into the searchBar which they select and add to the cart. Then user clicks on cart, changing from products page to SC page. However, user then changes their mind and returns to product page to change their order.

When this happens, previous text entered in SearchBar will still be displayed. I have tried SearchBar.Text = String.Empty;

on the OnDisappearing(), but that just sets the text to empty and for some reason the products page then stops working, I cant scroll up and down the LV when I try this.

Also tried SearchBar.Text = String.Empty; SearchBar.Unfocus();

but that didnt work either,

what about

SearchBar.ClearValue(what am I meant to put here? when I am only accessing it from..

 protected override void OnDisappearing()
        {       
//would this resolve it?
            SearchBarControl.ClearValue(//how do I access bindable property that it is asking for? when its not an event callback?
       
        }

am I mssing something? shouldnt there be something along the lines of SearchBar.Clear()?

thank you

have you tried to use this:

protected override void OnAppearing()
    {
        base.OnAppearing();
        SearchBar.Text = "";  //<<-- add this
    }

also makes sure in SearchBar_TextChanged

if (!String.IsNullOrWhiteSpace(SearchBar.Text)) 
    {
        //this code will be executed when there's a text in searchbar
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM