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

Use a UISearchBar and Search Controller on Xamarin.iOS

我目前有一个包含项目列表的应用程序,我现在希望能够搜索该列表。我知道我需要使用搜索栏和控制器,唯一的问题是,我找不到任何文档或示例来实现它。我为搜索栏设置了控制器 class,但它是空白的 class。哪里是一个好的起点?

This question 似乎是个好地方,但它去哪里了,我如何将它移植到 C# for xamarin?

这是一个 iOS 示例应用程序,演示了如何使用 UISearchController。搜索控制器管理搜索栏的呈现(与结果视图控制器的内容一致) 看看这个,如果之后有任何问题,请告诉我。 https://github.com/xamarin/monotouch-samples/tree/master/ios8/TableSearch

我使用 Storyboard 和 UISearchDisplayControlelr 为您创建了这个演示。

看看这个SearchDemo

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

将委托 class 添加到 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;
            }
        }