private async void QueryEvents(string searchTerms) { //do an async query to our REST endpoint string searchUrl = "https://socialevents.azurewebsites.net/api/events/search?tagName=" + searchTerms; HttpClient hc = new HttpClient(); string data = await hc.GetStringAsync(searchUrl); //if we got some data back then try and load it into our set of search results of //social events and Yammer messages if (!string.IsNullOrEmpty(data)) { CortanaSearchResult csr = CortanaSearchResult.GetInstanceFromJson(data); Dispatcher.BeginInvoke(() => { //if we have some data then plug into our UI if ((csr != null) && ((csr.Events.Count > 0) || (csr.YammerMessages.Count > 0))) { WaitBorder.Visibility = System.Windows.Visibility.Collapsed; WaitImg.Visibility = System.Windows.Visibility.Collapsed; WaitTxt.Visibility = System.Windows.Visibility.Collapsed; EventsLst.Visibility = System.Windows.Visibility.Visible; EventsLst.DataContext = csr.Events; YammerLst.DataContext = csr.YammerMessages; } else { //update the UI to show that there were no search results found WaitBorder.Visibility = System.Windows.Visibility.Visible; WaitImg.Visibility = System.Windows.Visibility.Visible; WaitTxt.Visibility = System.Windows.Visibility.Visible; string notFoundMessage = "Sorry, I couldn't find any results for \"" + searchTerms + "\""; WaitTxt.Text = notFoundMessage; string spokenMessage = HttpUtility.HtmlEncode(notFoundMessage); StartSpeakingSsml(String.Format(AppResources.SpokenShortSearchEmptyTemplate, searchTerms)); } SearchTextBox.Text = ""; }); } }
public static CortanaSearchResult GetInstanceFromJson(string data) { CortanaSearchResult returnDataType = new CortanaSearchResult(); try { MemoryStream ms = new MemoryStream(); byte[] buf = System.Text.UTF8Encoding.UTF8.GetBytes(data); ms.Write(buf, 0, Convert.ToInt32(buf.Length)); ms.Position = 0; DataContractJsonSerializer s = new DataContractJsonSerializer(typeof(CortanaSearchResult)); returnDataType = (CortanaSearchResult)s.ReadObject(ms); } catch (Exception ex) { Console.WriteLine("Error getting instance from JSON: " + ex.Message); } return(returnDataType); }