標籤:des style blog http io ar color os sp
一:建立一個普通的WP8.1應用
二:在解決方案選中項目,右擊 “添加NuGet應用”,添加 json.net
三:把抓包的JSON地址,處理一下
JSON格式線上校對:http://www.bejson.com/go.html?u=http://www.bejson.com/jsonviewernew/
JSON產生C#類:http://tools.wx6.org/json2csharp/
四:輸入相關的代碼並且和前台頁面綁定在一塊:
後台頁面的代碼:
using System;using System.Collections.Generic;using System.Linq;using System.Net;using System.Windows;using System.Windows.Controls;using System.Windows.Navigation;using Microsoft.Phone.Controls;using Microsoft.Phone.Shell;using PhoneApp1.Resources;using Newtonsoft.Json;namespace PhoneApp1{ public partial class MainPage : PhoneApplicationPage { // 建構函式 public MainPage() { InitializeComponent(); this.Loaded += MainPage_Loaded; // 用於本地化 ApplicationBar 的範例程式碼 //BuildLocalizedApplicationBar(); } void MainPage_Loaded(object sender, RoutedEventArgs e) { WebClient webClient = new WebClient(); webClient.DownloadStringAsync(new Uri("http://wireless.tianya.cn/v/focusStand/rank?type=1&pageSize=100", UriKind.Absolute)); webClient.DownloadStringCompleted += webClient_DownloadStringCompleted; } void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) { var root = JsonConvert.DeserializeObject<Root>(e.Result); lls.ItemsSource = root.Data.List; } private void lls_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (lls.SelectedItem != null) { var list = (lls.SelectedItem as List); MessageBox.Show(list.CategoryId); } } }}
前台頁面的代碼:
<!--ContentPanel - 在此處放置其他內容--> <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> <phone:LongListSelector x:Name="lls" SelectionChanged="lls_SelectionChanged"> <phone:LongListSelector.ItemTemplate> <DataTemplate> <StackPanel> <TextBlock Text="{Binding CategoryName}"></TextBlock> </StackPanel> </DataTemplate> </phone:LongListSelector.ItemTemplate> </phone:LongListSelector> </Grid>
完成。
源碼:http://pan.baidu.com/s/1qWM8FRQ
WP8.1開發筆記一:JSON資料處理