如何触发 SearchBar Xamarin 表单上的事件取消按钮

问题描述 投票:0回答:2

Xamarin 表单视图模型可以触发搜索栏的 onTextChange 事件,但没有 OnCancelButtonClicked 的事件处理程序。

我想要的:

只要单击取消/关闭按钮,就应该触发事件,如下所示。

xamarin xamarin.forms xamarin.android xamarin.ios xamarin-studio
2个回答
0
投票

您可以在SearchBar自定义渲染中获取Searchbar CloseButton事件,但我认为这对您的目标没有用。

我建议您可以点击搜索图标来刷新数据源。我用Searchbar和ListView做了一个示例,你可以看一下:

[assembly: ExportRenderer(typeof(SearchBar), typeof(CustomSearchBarRenderer))]
namespace FormsSample.Droid
{
 public class CustomSearchBarRenderer: SearchBarRenderer
{
    public CustomSearchBarRenderer(Context context):base(context)
    {

    }
    protected override void OnElementChanged(ElementChangedEventArgs<SearchBar> e)
    {
        base.OnElementChanged(e);

        if (Control != null)
        {
            var searchView = Control;
            searchView.Iconified = true;
            searchView.SetIconifiedByDefault(false);
            int searchCloseButtonId = Context.Resources.GetIdentifier("android:id/search_close_btn", null, null);

           // search close button icon, you can add event for closeIcon.click.
            var closeIcon = searchView.FindViewById(searchCloseButtonId);

            int searchViewSearchButtonId = Control.Resources.GetIdentifier("android:id/search_mag_icon", null, null);
            var searchIcon = searchView.FindViewById(searchViewSearchButtonId);
            searchIcon.Click += SearchIcon_Click;
        }        
       
    }

    private void SearchIcon_Click(object sender, EventArgs e)
    {
        Element.OnSearchButtonPressed();
       
    }
  
}
 }



 <StackLayout>
        <SearchBar
            x:Name="searchBar"
            HorizontalOptions="Fill"
            Placeholder="Search fruits..."
            SearchButtonPressed="OnSearchButtonPressed"
            VerticalOptions="CenterAndExpand" />
        <Label
            HorizontalOptions="Fill"
            Text="Enter a search term and press enter or click the magnifying glass to perform a search."
            VerticalOptions="CenterAndExpand" />
        <ListView
            x:Name="searchResults"
            HorizontalOptions="Fill"
            VerticalOptions="CenterAndExpand" />
    </StackLayout>

 public partial class Page23 : ContentPage
{
    public Page23()
    {
        InitializeComponent();
        searchResults.ItemsSource = DataService.Fruits;
    }

    private void OnSearchButtonPressed(object sender, EventArgs e)
    {
        if(string.IsNullOrEmpty(searchBar.Text))
        {
            searchResults.ItemsSource = DataService.Fruits;
        }
        else
        {
            searchResults.ItemsSource = DataService.GetSearchResults(searchBar.Text);
        }
       
    }
}

您可以先点击搜索关闭按钮移动SearchBar中的文本,然后点击SearchButton刷新listView的数据。


0
投票

嗯,因为该按钮只是清除搜索栏,当文本更改时它会触发一个事件,我刚刚添加了一个 if 来验证文本是否为空或为空,并且它对我有用:P

    private void SearchBarPatients_TextChanged(object sender, TextChangedEventArgs e)
    {
        if(string.IsNullOrWhiteSpace(SearchBarPatients.Text))
        {
            listPatients.ItemsSource = patients;
        }
    }

我知道这是你的疑问,我也有这个,所以就这么做了。 顺便说一句,我是个新手,如果它错了,我希望得到纠正:P

© www.soinside.com 2019 - 2024. All rights reserved.