简体   繁体   English

在Xamarin.iOS上使用UISearchBar和搜索控制器

[英]Use a UISearchBar and Search Controller on Xamarin.iOS

I currently have an app that contains a list of items, I now want to be able to search that list. 我目前有一个包含项目列表的应用程序,现在我希望能够搜索该列表。 I know Ill need to use a Search Bar and Controller, the only thing is, I can't find any documentation or examples for implementing this. 我知道我需要使用搜索栏和控制器,唯一的是,我找不到实现此目的的任何文档或示例。 I have a controller class setup for the search bar, but its a blank class. 我为搜索栏设置了一个控制器类,但是它是一个空白类。 Where is a good starting point for this? 哪里有一个好的起点?

This question seems to be a good spot, but where does that go, and how do I port that to C# for xamarin? 这个问题似乎是一个好地方,但是它去了哪里,以及如何将其移植到xamarin的C#中呢?

Xamarin 5.10: Xamarin 5.10:

    var sampleSearchBar = new UISearchBar (new CoreGraphics.CGRect (20, 20, this.View.Frame.Width - 50, 40));
sampleSearchBar.SearchBarStyle = UISearchBarStyle.Prominent;

 sampleSearchBar.ShowsCancelButton = true;

    //Deleagte class source
  sampleSearchBar.Delegate = new SearchDelegate ();
 this.View.AddSubview (sampleSearchBar);

Add Delegate class to the ViewController. 将Delegate类添加到ViewController。

class SearchDelegate : UISearchBarDelegate
        {
            public override void SearchButtonClicked (UISearchBar bar)
            {
                bar.ResignFirstResponder ();
            }

            public override void CancelButtonClicked (UISearchBar bar)
            {
                bar.ResignFirstResponder ();
            }

            public override bool ShouldBeginEditing (UISearchBar searchBar)
            {

                return true;
            }

            public override bool ShouldEndEditing (UISearchBar searchBar)
            {
                return true;
            }

            public override bool ShouldChangeTextInRange (UISearchBar searchBar, NSRange range, string text)
            {
                Console.WriteLine (searchBar.Text);
                return true;
            }
        }

This is an iOS sample application that demonstrates how to use UISearchController. 这是一个iOS示例应用程序,演示了如何使用UISearchController。 A search controller manages the presentation of a search bar (in concert with the results view controller's content) Look at this and let me know if any issue after that. 搜索控制器管理搜索栏的显示(与结果视图控制器的内容一致)。看一下这个,然后告诉我是否有任何问题。 https://github.com/xamarin/monotouch-samples/tree/master/ios8/TableSearch https://github.com/xamarin/monotouch-samples/tree/master/ios8/TableSearch

I create this demo for you with Storyboard and UISearchDisplayControlelr. 我使用Storyboard和UISearchDisplayControlelr为您创建此演示。

Look at this SearchDemo . 看一下这个SearchDemo

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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