【问题标题】:xamarin forms - clear SearchBarxamarin 表单 - 清除 SearchBar
【发布时间】:2021-05-06 19:30:59
【问题描述】:
<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> 

为什么没有清除'SearchBar'的事件

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

用户在搜索栏中输入产品并选择并添加到购物车的场景。然后用户点击购物车,从产品页面切换到 SC 页面。但是,用户随后改变主意并返回产品页面更改订单。

发生这种情况时,之前在 SearchBar 中输入的文本仍将显示。 我努力了 SearchBar.Text = String.Empty;

在 OnDisappearing() 上,但这只是将文本设置为空,并且由于某种原因产品页面停止工作,我尝试此操作时无法上下滚动 LV。

也试过了 SearchBar.Text = String.Empty; SearchBar.Unfocus();

但这也没用,

怎么样

SearchBar.ClearValue(我想在这里放什么?当我只从...访问它时..

 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?
       
        }

我在发什么消息吗?不应该有类似 SearchBar.Clear() 的东西吗?

谢谢

【问题讨论】:

  • 只需设置searchBar.Text = string.Empty;。如果这导致代码中的其他地方出现问题,请解决该问题。
  • 肯定的....

标签: c# xaml xamarin xamarin.forms searchbar


【解决方案1】:

你试过用这个吗:

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

还确保在 SearchBar_TextChanged 中

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

【讨论】:

  • 嗨,Godya,非常感谢您的回复。原来我没有在我的 SearchBar_TextChanged 中设置 if (!String.IsNullOrWhiteSpace(SearchBar.Text))。再次感谢这解决了我的问题。
猜你喜欢
  • 1970-01-01
  • 2017-06-24
  • 2021-07-24
  • 2016-08-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多