public static Jokes Jokes_JsonConvert(string json, int i)//笑话Json反序列化 { Jokes jokes = new Jokes(); try {//文字笑话 JObject jo1 = (JObject)JsonConvert.DeserializeObject(json); JObject jo2 = (JObject)jo1["showapi_res_body"]; JArray ja1 = (JArray)jo2["contentlist"]; jokes.title = ja1[i]["title"].ToString(); jokes.text = ja1[i]["text"].ToString(); jokes.ct = ja1[i]["ct"].ToString(); return(jokes); } catch (Exception) {//图片笑话 JObject jo1 = (JObject)JsonConvert.DeserializeObject(json); JObject jo2 = (JObject)jo1["showapi_res_body"]; JArray ja1 = (JArray)jo2["contentlist"]; jokes.title = ja1[i]["title"].ToString(); jokes.ct = ja1[i]["ct"].ToString(); jokes.img = ja1[i]["img"].ToString(); return(jokes); throw; } }
private void ReFresh_List(string jokes_json, int m)//刷新笑话列表 { try { all_page = JsonToObject.All_Page("Jokes", this.jokes_json);//该关键字总共包含的页数 if (now_page > all_page) { return; } else { if (m == 0) { App.jokeslists = new ObservableCollection <Jokes>();//清空newslists中的内容 } for (int i = 0; i < 20; i++) { jokes = JsonToObject.Jokes_JsonConvert(this.jokes_json, i); App.jokeslists.Add(jokes); } } jokes_listview.ItemsSource = App.jokeslists;//绑定列表 } catch (Exception) { throw; } }
} //图片笑话数据模板 protected override DataTemplate SelectTemplateCore(object item, DependencyObject container) //重写DataTemplateSelector的虚方法SelectTemplateCore方法 { Jokes jokes_item = item as Jokes; if (jokes_item.text == null) { return(Jokes_Img_ItemTemplate); } else if (jokes_item.img == null) { return(Jokes_Text_ItemTemplate); } return(null);//返回空 }