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

flseospp  于 5个月前  发布在  其他
关注(0)|答案(2)|浏览(62)

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

x1c 0d1x的数据

我想要的:

每当点击取消/关闭按钮时,应触发事件,如下所示。


trnvg8h3

trnvg8h31#

你可以在SearchBar自定义渲染中获得Searchbar CloseButton事件,但我认为它对你的目标没有用处。
我建议你可以点击搜索图标来刷新数据源。我给你一个使用搜索栏和列表视图的例子,你可以看看:

[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);
        }
       
    }
}

字符串
您可以先单击搜索关闭按钮移动搜索栏中的文本,然后单击搜索按钮刷新列表视图的数据。


的数据

t5fffqht

t5fffqht2#

好吧,因为这个按钮只是清除搜索栏,当文本改变时它会触发一个事件,我只是添加了一个if来验证文本是否为null或空,它对我很有效:P

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

字符串
我明白这是你的疑问,我也有这个,所以就这样做了。顺便说一句,我是这样一个新手,如果它是错误的,我希望得到纠正:P

相关问题