允许用户在 Windows 应用商店应用(C#/VB/C++ 和 XAML)中搜索信息
若要允许用户搜索应用中的信息,请将“搜索结果页面”项添加到项目中。然后,将 SearchBox**** 控件添加到应用中的任一页面。 用户通过这些控件搜索应用中的信息。若要允许用户搜索应用中的信息,请执行以下步骤:
- 添加搜索结果页面项。
- 修改搜索结果页面项中的代码。
- 向页面添加 SearchBox 控件。
- 允许用户打开搜索结果中出现的项。
- 测试应用中的搜索功能。
添加搜索结果页面项
在 Visual Studio 菜单上,单击“文件”>“新建项目”****。
在“新建项目”对话框的左侧窗格中,单击 Visual C#、Visual Basic 或 Visual C++ 节点。
在中心窗格中,单击“网格应用”。
在“名称”框中,输入 SearchPageExample,然后单击“确定”按钮。****
此刻,解决方案已创建并且项目文件显示在“解决方案资源管理器”中。有关项目文件的详细信息,请参阅适用于 Windows 应用商店应用的 C#、VB 以及 C++ 项目模板。
要点 首次运行 Visual Studio 时,它会提示你获取开发者许可证。有关详细信息,请参阅获取开发者许可证。
在“解决方案资源管理器”中,打开项目节点的快捷菜单,然后单击“添加”>“新建项目”。****
在“添加新项目”对话框的中心窗格中,单击“搜索结果页面”。对于此示例,保留默认名称 SearchResultsPage1.xaml,该名称会出现在“名称”框中。
单击“添加”按钮。
随即将显示新文件的代码
修改搜索结果页面项中的代码
在 SearchResultsPage1 代码文件中,将以下语句添加到文件顶部以引用示例数据类:
using SearchResultsExample.Data;
Imports SearchResultsExample.Data
添加以下属性:
public Dictionary<String, IEnumerable<SampleDataItem>> Results { get; set; }
Public Property Results() As Dictionary(Of [String], IEnumerable(Of SampleDataItem)) Get Return m_Results End Get Set(value As Dictionary(Of [String], IEnumerable(Of SampleDataItem))) m_Results = Value End Set End Property Private m_Results As Dictionary(Of [String], IEnumerable(Of SampleDataItem))
通过替换
// TODO: Application-specific searching logic. The search process is responsible for // creating a list of user-selectable result categories: // // filterList.Add(new Filter("<filter name>", <result count>)); // // Only the first filter, typically "All", should pass true as a third argument in // order to start in an active state. Results for the active filter are provided // in Filter_SelectionChanged below. var filterList = new List<Filter>(); filterList.Add(new Filter("All", 0, true));
' TODO: Application-specific searching logic. The search process is responsible for ' creating a list of user-selectable result categories: ' ' filterList.Add(new Filter("<filter name>", <result count>)); ' ' Only the first filter, typically "All", should pass true as a third argument in ' order to start in an active state. Results for the active filter are provided ' in Filter_SelectionChanged below. Dim filterList As New List(Of Filter)() filterList.Add(New Filter("All", 0, True))
来更新第一个
TODO
注释替换为
var filterList = new List<Filter>(); var totalMatchingItems = 0; Results = new Dictionary<string, IEnumerable<SampleDataItem>>(); var groups = await SampleDataSource.GetGroupsAsync(); foreach (var group in groups) { var matchingItems = group.Items.Where(item => item.Title.Contains(queryText)); int numberOfMatchingItems = matchingItems.Count(); if (numberOfMatchingItems > 0) { Results.Add(group.Title, matchingItems); filterList.Add(new Filter(group.Title, numberOfMatchingItems)); } } filterList.Insert(0, new Filter("All", totalMatchingItems, true));
Dim filterList = New List(Of Filter)() Dim totalMatchingItems = 0 Results = New Dictionary(Of String, IEnumerable(Of SampleDataItem))() Dim groups As IEnumerable(Of Data.SampleDataGroup) = Await Data.SampleDataSource.GetGroupsAsync() For Each group In groups Dim matchingItems = group.Items.Where(Function(item) item.Title.Contains(queryText)) Dim numberOfMatchingItems As Integer = matchingItems.Count() If numberOfMatchingItems > 0 Then Results.Add(group.Title, matchingItems) filterList.Add(New Filter(group.Title, numberOfMatchingItems)) End If Next filterList.Insert(0, New Filter("All", totalMatchingItems, True))
通过替换
// TODO: Respond to the change in active filter by setting this.DefaultViewModel["Results"] // to a collection of items with bindable Image, Title, Subtitle, and Description properties
' TODO: Respond to the change in active filter by setting Me.DefaultViewModel["Results"] ' to a collection of items with bindable Image, Title, Subtitle, and Description properties
来更新下一个
TODO
注释替换为
if (selectedFilter.Name.Equals("All")) { var tempResults = new List<SampleDataItem>(); foreach (var group in Results) { tempResults.AddRange(group.Value); this.DefaultViewModel["Results"] = tempResults; } } else if (Results.ContainsKey(selectedFilter.Name)) { this.DefaultViewModel["Results"] = new List<SampleDataItem>(Results[selectedFilter.Name]); }
If selectedFilter.Name.Equals("All") Then Dim tempResults = New List(Of SampleDataItem)() For Each group In m_Results tempResults.AddRange(group.Value) Me.DefaultViewModel("Results") = tempResults Next ElseIf m_Results.ContainsKey(selectedFilter.Name) Then Me.DefaultViewModel("Results") = New List(Of SampleDataItem)(m_Results(selectedFilter.Name)) End If
向页面添加 SearchBox 控件
在“解决方案资源管理器”中,双击要将搜索框添加到的页面(例如:GroupItemsPage.xaml)。
该页面会在设计器中打开。
在 Toolbox ****中,将 SearchBox 控件拖动到该页面。
在“属性”窗口中,单击“事件”选项卡,然后在 QuerySubmitted**** 字段中,键入 SearchBox_QuerySubmitted,然后按 Enter 键。
SearchBox_QuerySubmitted 事件处理程序会在代码编辑器中打开。
使用以下代码替换 SearchBox_QuerySubmitted**** 事件处理程序:
private void SearchBox_QuerySubmitted(SearchBox sender, SearchBoxQuerySubmittedEventArgs args) { this.Frame.Navigate(typeof(SearchResultsPage1), args.QueryText); }
Private Sub SearchBox_QuerySubmitted(sender As SearchBox, args As SearchBoxQuerySubmittedEventArgs) Me.Frame.Navigate(GetType(SearchResultsPage1), args.QueryText) End Sub
允许用户打开搜索结果中出现的项
在设计器重打开 SearchResultsPage1.xaml。
在“文档大纲”面板中,单击 resultsGridView。
在“属性”窗口中,单击“事件”选项卡,然后在 ItemClick 字段中,键入 ItemClick。
将以下两行代码添加到新的事件处理程序中:
private void ItemClick(object sender, ItemClickEventArgs e) { var itemId = ((SampleDataItem)e.ClickedItem).UniqueId; this.Frame.Navigate(typeof(ItemDetailPage), itemId); }
Private Sub ItemClick(sender As Object, e As ItemClickEventArgs) Handles resultsGridView.ItemClick Dim itemId = DirectCast(e.ClickedItem, SampleDataItem).UniqueId Me.Frame.Navigate(GetType(ItemDetailPage), itemId) End Sub
测试应用中的搜索功能
在 Visual Studio 菜单中,单击“调试”>“开始调试”****,或按 F5。
在运行的应用中,打开包含搜索框控件的页面。
在搜索框中,键入项名称,然后单击“搜索”按钮。
搜索结果页面将显示包含此搜索词的项的结果。