public static async Task PopulateSuggestionsAsync(string queryText, SearchSuggestionCollection results) { // if we don't have at least three characters to work with, do nothing... if(queryText.Length < 3) return; // how many? int maxSuggestions = 5; // get the list... var suggestions = await ReportItem.GetSearchSuggestionsAsync(queryText); // sort the suggestions... var titles = new List<string>(); foreach (var title in suggestions.Keys) titles.Add(title); titles.Sort(); // do we have one that we can use as a recommendation? ReportItem recommendation = null; foreach (var title in titles) { if (suggestions[title].Count == 1) { recommendation = suggestions[title][0]; break; } } // if we have a recommendation only show three suggestions... if (recommendation != null) maxSuggestions -= 2; // add the suggestions... foreach (var title in titles) { results.AppendQuerySuggestion(title); // enough? if (results.Size == maxSuggestions) break; } // add the recommendation... if (recommendation != null) { // we need an image... var viewItem = new ReportViewItem(recommendation); var imageUri = await new ReportImageCacheManager().GetLocalImageUriAsync(viewItem); // add the suggestion... results.AppendSearchSeparator("Recommendation"); results.AppendResultSuggestion(recommendation.Title, recommendation.Description, recommendation.Id.ToString(), RandomAccessStreamReference.CreateFromUri(new Uri(imageUri)), recommendation.Title); } }
private async Task GetSuggestionsAsync(string str, SearchSuggestionCollection suggestions) { // Cancel the previous suggestion request if it is not finished. if (currentXmlRequestOp != null) { currentXmlRequestOp.Cancel(); } // Get the suggestion from a web service. currentXmlRequestOp = XmlDocument.LoadFromUriAsync(new Uri(str)); XmlDocument doc = await currentXmlRequestOp; await Task.Delay(rand.Next(10, 5000)); Debug.WriteLine("** response size: {0}", doc.GetXml().Length); currentXmlRequestOp = null; XmlNodeList nodes = doc.GetElementsByTagName("Section"); if (nodes.Count > 0) { IXmlNode section = nodes[0]; foreach (IXmlNode node in section.ChildNodes) { if (node.NodeType != NodeType.ElementNode) { continue; } if (node.NodeName.Equals("Separator", StringComparison.CurrentCultureIgnoreCase)) { string title = null; IXmlNode titleAttr = node.Attributes.GetNamedItem("title"); if (titleAttr != null) { title = titleAttr.NodeValue.ToString(); } suggestions.AppendSearchSeparator(string.IsNullOrWhiteSpace(title) ? "Suggestions" : title); } else { AddSuggestionFromNode(node, suggestions); } } } }
private async Task GenerateSuggestions(StorageFile file, SearchSuggestionCollection suggestions) { XmlDocument doc = new XmlDocument(); doc.LoadXml(await FileIO.ReadTextAsync(file)); XmlNodeList nodes = doc.GetElementsByTagName("Section"); if (nodes.Count > 0) { IXmlNode section = nodes[0]; foreach (IXmlNode node in section.ChildNodes) { if (node.NodeType != NodeType.ElementNode) { continue; } if (node.NodeName.Equals("Separator", StringComparison.CurrentCultureIgnoreCase)) { string title = node.Attributes.GetNamedItem("title").NodeValue.ToString(); if (string.IsNullOrWhiteSpace(title)) { suggestions.AppendSearchSeparator(title); } } else { AddSuggestionFromNode(node, suggestions); } } } }