private async Task GetSuggestionsAsync(string str, SearchSuggestionCollection suggestions) { // Cancel the previous suggestion request if it is not finished. if (currentHttpTask != null) { currentHttpTask.AsAsyncOperation<string>().Cancel(); //Debug.WriteLine(" - cancelling task {0} status: {1}", currentHttpTask.Id, currentHttpTask.Status); } // Get the suggestions from an open search service. currentHttpTask = httpClient.GetStringAsync(str); Debug.WriteLine(" + task created, id: {0} string: {1}", currentHttpTask.Id, str); string response = await currentHttpTask; // inject a delay to simulate the time for the web response await Task.Delay(rand.Next(50, 6000)); JsonArray parsedResponse = JsonArray.Parse(response); if (parsedResponse.Count > 1) { foreach (JsonValue value in parsedResponse[1].GetArray()) { suggestions.AppendQuerySuggestion(value.GetString()); //Debug.WriteLine("\n Search Result ({0}): {1}", str, value.GetString()); if (suggestions.Size >= MainPage.SearchPaneMaxSuggestions) { break; } } } }
private void AddSuggestionFromNode(IXmlNode node, SearchSuggestionCollection suggestions) { string text = ""; string description = ""; string url = ""; string imageUrl = ""; string imageAlt = ""; foreach (IXmlNode subNode in node.ChildNodes) { if (subNode.NodeType != NodeType.ElementNode) { continue; } if (subNode.NodeName.Equals("Text", StringComparison.CurrentCultureIgnoreCase)) { text = subNode.InnerText; } else if (subNode.NodeName.Equals("Description", StringComparison.CurrentCultureIgnoreCase)) { description = subNode.InnerText; } else if (subNode.NodeName.Equals("Url", StringComparison.CurrentCultureIgnoreCase)) { url = subNode.InnerText; } else if (subNode.NodeName.Equals("Image", StringComparison.CurrentCultureIgnoreCase)) { if (subNode.Attributes.GetNamedItem("source") != null) { imageUrl = subNode.Attributes.GetNamedItem("source").InnerText; } if (subNode.Attributes.GetNamedItem("alt") != null) { imageAlt = subNode.Attributes.GetNamedItem("alt").InnerText; } } } if (string.IsNullOrWhiteSpace(text)) { // No proper suggestion item exists } else if (string.IsNullOrWhiteSpace(url)) { suggestions.AppendQuerySuggestion(text); } else { // The following image should not be used in your application for Result Suggestions. Replace the image with one that is tailored to your content Uri uri = string.IsNullOrWhiteSpace(imageUrl) ? new Uri("ms-appx:///Assets/SDK_ResultSuggestionImage.png") : new Uri(imageUrl); RandomAccessStreamReference imageSource = RandomAccessStreamReference.CreateFromUri(uri); suggestions.AppendResultSuggestion(text, description, url, imageSource, imageAlt); Debug.WriteLine("\n Search Result ({0}): {1}", text, description); } }
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); } }
/// <summary> /// Completes the retreval of suggestions from the web service /// </summary> /// <param name="str">User query string</param> /// <param name="suggestions">Suggestions list to append new suggestions</param> /// <returns></returns> private async Task GetSuggestionsAsync(string str, SearchSuggestionCollection suggestions) { // Cancel the previous suggestion request if it is not finished. if (currentHttpTask != null) { currentHttpTask.AsAsyncOperation<string>().Cancel(); } // Get the suggestions from an open search service. currentHttpTask = httpClient.GetStringAsync(str); string response = await currentHttpTask; JsonArray parsedResponse = JsonArray.Parse(response); if (parsedResponse.Count > 1) { foreach (JsonValue value in parsedResponse[1].GetArray()) { suggestions.AppendQuerySuggestion(value.GetString()); } } }
/// <summary> /// Completes the retreval of suggestions from the web service /// </summary> /// <param name="str">User query string</param> /// <param name="suggestions">Suggestions list to append new suggestions</param> /// <returns></returns> private async Task GetSuggestionsAsync(string str, SearchSuggestionCollection suggestions) { // Cancel the previous suggestion request if it is not finished. if (currentHttpTask != null) { currentHttpTask.AsAsyncOperation<string>().Cancel(); } // Get the suggestions from an open search service. currentHttpTask = httpClient.GetStringAsync(str); string response = await currentHttpTask; JsonObject jsonObject = JsonObject.Parse(response); jsonObject.ContainsKey("data"); String test1 = jsonObject["data"].GetString(); string[] test = test1.Split('|'); foreach (string value in test) { suggestions.AppendQuerySuggestion(value); } }
private void addToSearchResults(JsonArray jsonArray, SearchSuggestionCollection suggestionResult, int maxResults) { if (jsonArray.Count > 1) { foreach (JsonValue value in jsonArray[1].GetArray()) { suggestionResult.AppendQuerySuggestion(value.GetString()); Debug.WriteLine("Suggestion: " + value.GetString()); if (suggestionResult.Size >= MainPage.SearchPaneMaxSuggestions) { break; } } } }