UISearchController 和 MvvmCross
UISearchController and MvvmCross
我想为我的应用程序添加搜索逻辑 (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 中阅读了有关搜索的内容,并为 IOS8 应用程序选择了 UISearchController
。但我不明白,如何将此控制器添加到我的视图中:(
我从 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.searchTableController我应该做什么?我需要将我的显示逻辑移到那里吗?
是的。 "searchTableController" 应该负责搜索结果的呈现。
Here is the test project (native, not xmarin) which help you understand.
searchController 管理 "searchBar" 和 "searchResultPresenter"。他不需要添加到运营商控制器的视图层次结构中。当用户开始在 "searchBar" 中键入文本时,"SearchController" 会自动为您显示 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) 在搜索presenter中需要重新加载一个table方法"dataFound:"
- (void)dataFound:(NSArray *)searchResults
{
_searchResults = searchResults;
[self.tableView reloadData];
}
这里有一些关于如何将 UISearchController
与 Xamarin.iOS 结合使用的建议。
为结果 table 创建一个 新的 class 查看子 classing UITableViewSource
。这将是负责显示结果的视图。您需要制作 table 视图的项目列表 public.
public List<string> SearchedItems { get; set; }
在主 UIViewController
中,创建 UISearchController
并将结果 table 视图作为参数传递。我添加了一些额外的设置。
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;
}
在我看来,就用户体验而言,将搜索栏添加到 UI 的最佳方式是将其作为 NavigationItem
添加到 NavigationBarController
。
NavigationItem.TitleView = SearchController.SearchBar;
在主程序中添加执行搜索的方法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.
}
真心希望对你有所帮助,祝你好运。
我想为我的应用程序添加搜索逻辑 (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 中阅读了有关搜索的内容,并为 IOS8 应用程序选择了 UISearchController
。但我不明白,如何将此控制器添加到我的视图中:(
我从 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.searchTableController我应该做什么?我需要将我的显示逻辑移到那里吗?
是的。 "searchTableController" 应该负责搜索结果的呈现。
Here is the test project (native, not xmarin) which help you understand.
searchController 管理 "searchBar" 和 "searchResultPresenter"。他不需要添加到运营商控制器的视图层次结构中。当用户开始在 "searchBar" 中键入文本时,"SearchController" 会自动为您显示 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) 在搜索presenter中需要重新加载一个table方法"dataFound:"
- (void)dataFound:(NSArray *)searchResults
{
_searchResults = searchResults;
[self.tableView reloadData];
}
这里有一些关于如何将 UISearchController
与 Xamarin.iOS 结合使用的建议。
为结果 table 创建一个 新的 class 查看子 classing
UITableViewSource
。这将是负责显示结果的视图。您需要制作 table 视图的项目列表 public.public List<string> SearchedItems { get; set; }
在主
UIViewController
中,创建UISearchController
并将结果 table 视图作为参数传递。我添加了一些额外的设置。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; }
在我看来,就用户体验而言,将搜索栏添加到 UI 的最佳方式是将其作为
NavigationItem
添加到NavigationBarController
。NavigationItem.TitleView = SearchController.SearchBar;
在主程序中添加执行搜索的方法
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. }
真心希望对你有所帮助,祝你好运。