UISearchController and MvvmCross

本文关键字:MvvmCross and UISearchController | 更新日期: 2023-09-27 18:04:01

我想为我的应用程序(IOS8)添加搜索逻辑。我有简单的MvxTableViewController,并通过UITableViewSource显示我的数据。这里是:控制器:

    MvxViewFor(typeof(MainViewModel))]
    partial class MainController : MvxTableViewController
    {
        public MainController(IntPtr handle) : base(handle) { }

        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            // make background trasnsparent page 
            this.View.BackgroundColor = UIColor.Clear;
            this.TableView.BackgroundColor = UIColor.Clear;
            this.NavigationController.NavigationBar.BarStyle = UIBarStyle.Black;
            this.SetBackground ();
           (this.DataContext as MainViewModel).PropertyChanged += this.ViewModelPropertyChanged;
        }
        private void SetBackground()
        {
            // set blured bg image
        }
        private void ViewModelPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            var viewModel = this.ViewModel as MainViewModel;
            if (e.PropertyName == "Title")
            {
                this.Title = viewModel.Title;
            }
            else if (e.PropertyName == "Topics")
            {
                var tableSource = new TopicTableViewSource(viewModel.Topics);
                tableSource.TappedCommand = viewModel.NavigateToChildrenPageCommand;
                this.TableView.Source = tableSource;
                this.TableView.ReloadData();
            }
        }

我在IOS中阅读搜索并选择UISearchController为IOS8应用程序。但我不明白,我如何将此控制器添加到我的视图:(我从Xamarin (TableSearch)找到了样本-但他们不使用UITableViewSource,我不明白我应该怎么做。我试着添加控制器:

this.searchController = new UISearchController (this.searchTableController) 
{
       WeakDelegate = this,
       DimsBackgroundDuringPresentation = false,
       WeakSearchResultsUpdater = this,
};
this.searchController.SearchBar.SizeToFit ();
this.TableView.TableHeaderView = searchController.SearchBar;
this.TableView.WeakDelegate = this;
this.searchController.SearchBar.WeakDelegate = this;

我应该在this。searchtable controller中做什么?我需要把我的显示逻辑移到那里吗?

UISearchController and MvvmCross

是。"searchTableController"应该负责搜索结果的呈现。

下面是测试项目(原生的,不是xmarin的),它可以帮助您理解。

searchController管理一个"searchBar"answers"searchResultPresenter"。它不需要添加到载体控制器的视图层次结构中。当用户开始在"搜索栏"中输入文本时,"搜索控制器"自动为您显示SearchResultPresenter。

步骤:1)用SearchResultsPresenterController实例化搜索控制器。

2)当用户在搜索栏中输入文本时,您应该为搜索调用自己的服务。下面是代码示例…

#pragma mark - UISearchResultsUpdating
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
    NSString *searchString = searchController.searchBar.text;
    if (searchString.length > 1)
    {
        // TODO - call your service for the search by string
        // this may be async or sync
        // When a data was found - set it to presenter
        [self.searchResultPresenter dataFound:<found data>];
    }
}

3)在搜索演示者需要在方法" dataffound:"中重新加载一个表

- (void)dataFound:(NSArray *)searchResults
{
    _searchResults = searchResults;
    [self.tableView reloadData];
}

以下是关于如何在Xamarin.iOS中使用UISearchController的一些建议。

  1. 为结果表视图子类UITableViewSource创建一个新类这是负责显示结果的视图。您需要将该表视图的items列表设置为public

    public List<string> SearchedItems { get; set; }
    
  2. 在您的主UIViewController中,创建您的UISearchController并将您的结果表视图作为参数传递。我添加了一些额外的设置。

    public UISearchController SearchController { get; set; }
    public override void ViewDidLoad ()
    {
        SearchController = new UISearchController (resultsTableController) {
            WeakDelegate = this,
            DimsBackgroundDuringPresentation = false,
            WeakSearchResultsUpdater = this,
        };
        SearchController.SearchBar.SizeToFit ();
        SearchController.SearchBar.WeakDelegate = this;
        SearchController.HidesNavigationBarDuringPresentation = false;
        DefinesPresentationContext = true;
    }
    
  3. 在我看来,在用户体验方面,将搜索栏添加到UI的最佳方法是将其作为NavigationItem添加到NavigationBarController

    NavigationItem.TitleView = SearchController.SearchBar;
    
  4. 添加在主UIViewController中执行搜索的方法:

    [Export ("updateSearchResultsForSearchController:")]
    public virtual void UpdateSearchResultsForSearchController (UISearchController searchController)
    {
        var tableController = (UITableViewController)searchController.SearchResultsController;
        var resultsSource = (ResultsTableSource)tableController.TableView.Source;
        resultsSource.SearchedItems = PerformSearch (searchController.SearchBar.Text);
        tableController.TableView.ReloadData ();
    }
    static List<string> PerformSearch (string searchString)
    {
        // Return a list of elements that correspond to the search or
        // parse an existing list.
    }
    

我真的希望这对你有帮助,祝你好运。