C# UISearchController和MvvmCross
我想为我的应用程序IOS8添加搜索逻辑。我有简单的MvxTableViewController,并通过UITableViewSource显示我的数据。这是: …控制器:C# UISearchController和MvvmCross,c#,ios,xamarin,xamarin.ios,C#,Ios,Xamarin,Xamarin.ios,我想为我的应用程序IOS8添加搜索逻辑。我有简单的MvxTableViewController,并通过UITableViewSource显示我的数据。这是: …控制器: MvxViewFor(typeof(MainViewModel))] partial class MainController : MvxTableViewController { public MainController(IntPtr handle) : base(handle) {
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();
}
}
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;
我阅读了有关在IOS中搜索的内容,并为IOS8应用程序选择了UISearchController。但我不明白如何将此控制器添加到我的视图中:
我从Xamarin表搜索中找到了一个样本,但他们不使用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();
}
}
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;
在这个.searchTableController中我应该做什么?是否需要将显示逻辑移到那里?是。searchTableController应负责显示搜索结果 searchController管理搜索栏和searchResultPresenter。His不需要添加到运营商控制器的视图层次结构中。当用户开始在搜索栏中键入文本时,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>];
}
}
下面是关于如何将UISearchController与Xamarin.iOS一起使用的一些建议 为结果表视图子类UITableViewSource创建一个新类。这将是负责显示结果的视图。您需要将该表视图的项目列表公开 在我看来,根据用户体验将搜索栏添加到UI的最佳方法是将其作为NavigationItem添加到NavigationBarController 添加在主UIViewController中执行搜索的方法:
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();
}
}
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;
我真的希望这对你有帮助,祝你好运。谢谢。我看到了这个样本,它是xamarin samle的类比。但在这些示例中,未使用UITableViewSource。“我不明白,我该如何组合,我该如何编辑我的逻辑以进行正确的搜索工作。”NikitaBondarenko补充了我理解的解释。我需要创建新的TableViewController来显示搜索结果。它应该是带有项目列表的简单控制器,并覆盖显示RowsIsSection和GetCell等的默认方法,或者使用更新此源的方法覆盖youself-UITableViewSource。谢谢,我明白了。我需要创建新的TableViewController来显示搜索结果。它应该是带有项目列表的简单控制器,并覆盖显示RowsIsSection和GetCell等的默认方法,或者使用更新此源的方法覆盖youself-UITableViewSource。谢谢,没错。注意:UITableViewSource只是一个结合了UITableViewDataSource和UITableViewDelegate的助手。
[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.
}