private async void Page_Loaded(object sender, RoutedEventArgs e)
        {
            //此集合为GridView的source
            ObservableCollection <WallpapersDetail> picModels = new ObservableCollection <WallpapersDetail>();
            //json文件的url
            Uri uri  = new Uri("ms-appx:///Assets/file.json");
            var file = await StorageFile.GetFileFromApplicationUriAsync(uri);

            //读取的json文本
            string text = await Windows.Storage.FileIO.ReadTextAsync(file);

            //然后反序列化成类
            WallpapersData wallPaperModel = Newtonsoft.Json.JsonConvert.DeserializeObject <WallpapersData>(text);

            //通过重新组装成集合给GridView
            foreach (var item in wallPaperModel.images)
            {
                picModels.Add(new WallpapersDetail()
                {
                    Title  = item.copyright,
                    Source = "https://www.bing.com" + item.url
                });
            }
            GV.ItemsSource = picModels;
        }
        public async Task <WallpapersData> GetWallparper(int index, int number)
        {
            // string url = "https://cn.bing.com/HPImageArchive.aspx?format=js&idx=8&n=25";
            string url = string.Format("https://cn.bing.com/HPImageArchive.aspx?format=js&idx={0}&n={1}&mkt=zh-cn", index, number);
            Uri    uri = new Uri(url);
            //var httpClient = new HttpClient();
            string json = await _httpClient.GetStringAsync(uri);

            WallpapersData wallPapersData = JsonConvert.DeserializeObject <WallpapersData>(json);

            return(wallPapersData);
        }
Exemple #3
0
        public async Task TestAsync(WallpaperService wallpaperService)
        {
            using (HttpClient _httpClient = new HttpClient())
            {
                // string url = "https://cn.bing.com/HPImageArchive.aspx?format=js&idx=8&n=25";
                string url = string.Format("https://cn.bing.com/HPImageArchive.aspx?format=js&idx={0}&n={1}&mkt=zh-cn", 0, 6);
                Uri    uri = new Uri(url);
                //var httpClient = new HttpClient();
                string json = await _httpClient.GetStringAsync(uri);

                WallpapersData wallPapersData = JsonConvert.DeserializeObject <WallpapersData>(json);
            }

            //var data = await wallpaperService.GetWallparper(0, 6);
            // to do...
        }