private static bool TryParse(XContainer xml, out GoodreadsWork result) { result = null; var workNode = (xml is XElement && (xml as XElement).Name == "work") ? xml as XElement : xml.Descendants("work").FirstOrDefault(); var idNode = (workNode.Element("id")?.FirstNode as XText)?.Value; int id; if (idNode == null || !int.TryParse(idNode, out id)) { id = int.MinValue; } var bookNode = workNode.Element("best_book"); GoodreadsBook book; if (bookNode == null || !GoodreadsBook.TryParse(bookNode, out book)) { return false; } var averageRatingNode = (workNode.Element("average_rating")?.FirstNode as XText)?.Value; float averageRating; if (averageRatingNode == null || !float.TryParse(averageRatingNode, out averageRating)) { result = new GoodreadsWork(id, book); } else { result = new GoodreadsWork(id, book, averageRating); } return true; }
private void DelayTimerCallback(object sender, EventArgs e) { if (Interlocked.Exchange(ref queryQueued, 1) == 0) { var requestString = GetRequestString(queryString); var results = SearchGoodreads(requestString).ToList(); this.okButton.Invoke(new Action(() => { this.okButton.Enabled = false; })); bool selected = false; this.selectionBox.Invoke(new Action(() => { this.selectionBox.ClearSelected(); this.selectionBox.Items.Clear(); foreach (var result in results) { this.selectionBox.Items.Add(result); if (!selected && result.Equals(this.selectedWork)) { this.selectionBox.SelectedItem = result; selected = true; } } if (selected == false) { this.selectedWork = null; } })); if (selected) { this.okButton.Invoke(new Action(() => { this.okButton.Enabled = true; })); } Interlocked.Exchange(ref queryQueued, 0); } }
private void selectionBox_Select(object sender, EventArgs e) { var box = sender as ListBox; this.selectedWork = box.SelectedItem as GoodreadsWork; this.okButton.Invoke(new Action(() => { this.okButton.Enabled = true; })); }