private void BookName_TextChanged(object sender, TextChangedEventArgs e) { this.zhifubao.Visibility = Visibility.Hidden; this.weixin.Visibility = Visibility.Hidden; // Task task = new Task(() => // { if (string.IsNullOrWhiteSpace(bookName.Text)) { bookListBox.Dispatcher.InvokeAsync(() => bookListBox.Items.Clear()); bookListBox.Dispatcher.InvokeAsync(() => bookListBox.Visibility = Visibility.Hidden); return; } // bookListBox.Items.Clear(); bookListBox.Dispatcher.InvokeAsync(() => bookListBox.Items.Clear()); bookListBox.Visibility = Visibility.Visible; bookTitleAndId.Clear(); string name = bookName.Text.ToString().Trim(); string url = string.Format("http://api.zhuishushenqi.com/book/auto-complete?query={0}", name); var getValue = Task.Run(() => { string json = common.GetPage(url); // var json = getValue; if (String.IsNullOrEmpty(json)) { MessageBox.Show("网络错误"); return; } var bookComplete = new CompleteTitle(); bookComplete = (CompleteTitle)common.FromJson("CompleteTitle", json); List <string> list = new List <string>(); var ok = bookComplete.ok; if (ok) { if (bookComplete.keywords.Length <= 0) { bookListBox.Dispatcher.InvokeAsync(() => bookListBox.Items.Clear()); bookListBox.Dispatcher.InvokeAsync(() => bookListBox.Items.Add("没有此图书")); // bookListBox.Items.Add("没有此图书"); } else { bookListBox.Dispatcher.InvokeAsync(() => bookListBox.Items.Clear()); foreach (var item in bookComplete.keywords) { list.Add(item); bookListBox.Dispatcher.InvokeAsync(() => bookListBox.Items.Add(item)); } } } }); }
/// <summary> /// 初始化详情界面,填充数据 /// </summary> public void initData() { try { // 点击自动搜索框,数据的id var selectedIndex = Int32.Parse(Application.Current.Properties["SelectedIndex"].ToString()); var bookList = (Book[])Application.Current.Properties["SearchBookList"]; downloadBook = bookList[selectedIndex]; bookTitle.Content = bookList[selectedIndex].title; bookAuthor.Content = bookList[selectedIndex].author; bookCat.Content = bookList[selectedIndex].cat; bookWordCount.Content = bookList[selectedIndex].wordCount + "字"; bookLatelyFollower.Content = bookList[selectedIndex].latelyFollower + " 人"; booklatestChapter.Content = bookList[selectedIndex].lastChapter; bookRetentionRatio.Content = bookList[selectedIndex].retentionRatio + " %"; bookShortInfo.Text = bookList[selectedIndex].shortIntro; string url = bookList[selectedIndex].cover; int urlStartIndex = url.IndexOf("http:"); if (urlStartIndex >= 0) { url = url.Substring(urlStartIndex); bookImage.Source = new BitmapImage(new Uri(url, UriKind.RelativeOrAbsolute)); // picturebox_cover.ImageLocation = url; //MessageBox.Show(bookList[bookId].cover); } // 获取书源 url = "http://api.zhuishushenqi.com/toc?view=summary&book=" + bookList[selectedIndex]._id + ""; //Task<string> getValue = common.GetPage(url); //var json = await getValue; var getValue = Task.Run(() => { string json = common.GetPage(url); //jsonArry var bookSource = JArray.Parse(json); if (String.IsNullOrEmpty(json)) { MessageBox.Show("网络错误"); return; } Dictionary <string, string> sourceList = new Dictionary <string, string>(); List <Source> sourceDetialList = new List <Source>(); foreach (var sourceItem in bookSource) { var info = sourceItem.ToObject <Source>(); sourceDetialList.Add(info); // 书源的名字 书源内书的id sourceList.Add(info.name, info._id); } // 章节列表 url = "http://api.zhuishushenqi.com/toc/" + sourceList.First().Value + "?view=chapters"; json = common.GetPage(url); chapter = (ChapterList)common.FromJson("ChapterList", json); if (String.IsNullOrEmpty(json)) { MessageBox.Show("网络错误"); return; } if (chapter.name == "优质书源") { MessageBox.Show("当前电子书不支持下载"); this.Dispatcher.InvokeAsync(() => this.Close()); return; } Application.Current.Properties["chapterList"] = chapter; ObservableCollection <ChapterList> list = new ObservableCollection <ChapterList>(); foreach (var item in chapter.chapters) { chapter = new ChapterList(); chapter.name = item.title; chapter.link = item.link; list.Add(chapter); } DownloadButton.Dispatcher.InvokeAsync(() => DownloadButton.IsEnabled = true); dataGrid.Dispatcher.InvokeAsync(() => dataGrid.ItemsSource = list); }); } catch (Exception ex) { MessageBox.Show("网络错误"); return; } }