Windows 8用户体验的关键特点之一是对超级按钮的使用。它响应轻扫或Windows徽标+C键,并从屏幕右侧滑出。这些按钮(“超级按钮”)为Windows应用商店应用提供了一种以一致方式在应用程序之间公开常用功能的手段。例如,如果您需要在应用程序中执行搜素,您可以选择搜索超级按钮并在搜索窗格中输入一个搜索条目。用户界面和调用上述界面的操作在每个应用程序中都是一样的。为了与另一个应用程序共享数据,您使用共享超级按钮。一个支持共享的应用程序就可以共享数据了。例如,一个绘图应用程序可以与其他支持共享的应用程序共享图画,或者Contoso Cookbook可以与其他支持共享的应用程序共享食谱,
在本实验中,您将为Contoso Cookbook添加搜索和共享支持。您将获得实现搜索和共享合约的第一手体验。您将同时学习这些合约如何为两个应用程序之间或应用程序与Windows自身之间提供更高层次的集成。
private void OnItemClick(object sender, ItemClickEventArgs e)
{
// Navigate to the page showing the recipe that was clicked
this.Frame.Navigate(typeof(ItemDetailPage), ((RecipeDataItem)e.ClickedItem).UniqueId);
}
5、在SearchResultsPage.xaml.cs的顶部添加以下using语句。
C#
using ContosoCookbook.Data;
6、接着向SearchResultsPage类添加以下字段。
C#
// Collection of RecipeDataItem collections representing search results
private Dictionary _results = new Dictionary();
7、转至LoadState方法并在“Communicate results through the view model.”注释前添加以下语句。 C#
// Search recipes and tabulate results
var groups = RecipeDataSource.GetGroups("AllGroups");
string query = queryText.ToLower();
var all = new List();
_results.Add("All", all);
foreach (var group in groups)
{
var items = new List();
_results.Add(group.Title, items);
foreach (var item in group.Items)
{
if (item.Title.ToLower().Contains(query) || item.Directions.ToLower().Contains(query))
{
all.Add(item);
items.Add(item);
}
}
filterList.Add(new Filter(group.Title, items.Count, false));
}
filterList[0].Count = all.Count;
注意:您刚添加的代码根据用户输入的查询文本对所有食谱标题和指南进行搜索。对每个匹配的食谱,它将向变量名为all的食谱列表添加一个RecipeDataItem,并在每个特定组中向变量名为items的食谱列表添加一个RecipeDataItem。上述列表在您刚声明的字典中被维护,并根据“All”, “Chinese”等组名称被标记。
上述代码同时填充搜索结果页面顶部的筛选列表(由filterList变量代表),筛选列表包含组名称,并显示每个组中匹配的食谱数。
8、找到Filter_SelectionChanged方法并向if子句(紧挨在// TODO: Respond to the change in active filter注释后)添加以下语句。
using Windows.ApplicationModel.Search;
2、在OnLaunched方法的开始处紧挨着加载食谱数据的RecipeDataSource.LoadLocalDataAsync或RecipeDataSource.LoadRemoteDataAsync的调用后,添加以下语句。 C#
// Register handler for SuggestionsRequested events from the search pane
SearchPane.GetForCurrentView().SuggestionsRequested += OnSuggestionsRequested;
3、现在向Application类添加以下方法。 C#
// Reinitialize the app if a new instance was launched for search
if (args.PreviousExecutionState == ApplicationExecutionState.NotRunning ||
args.PreviousExecutionState == ApplicationExecutionState.ClosedByUser ||
args.PreviousExecutionState == ApplicationExecutionState.Terminated)
{
// Load recipe data
await RecipeDataSource.LoadLocalDataAsync();
// Register handler for SuggestionsRequested events from the search pane
SearchPane.GetForCurrentView().SuggestionsRequested += OnSuggestionsRequested;