Inheritance: ISearchSuggestionCollection
        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());
                }
            }
        }
Example #5
0
        /// <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 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);
                    }
                }
            }
        }
Example #7
0
 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);
             }
         }
     }
 }
		public AppendToSearchSuggestionCollectionVisitor(SearchSuggestionCollection collection)
		{
			if (collection == null) throw new ArgumentNullException("collection");
			_collection = collection;
		}
 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;
             }
         }
     }
 }