前段時(shí)間寫了使用必應(yīng)壁紙的接口,只是通過(guò)本地的json文本演示了壁紙展示蕉朵,沒(méi)有涉及網(wǎng)絡(luò)獲取腥光,這回這篇文章修改了數(shù)據(jù)獲取部分关顷,換成了在線獲取,項(xiàng)目的結(jié)構(gòu)和上次的沒(méi)太大的區(qū)別武福,所以大家可以先看看上篇文章议双,然后再看這篇文章。
如下圖捉片,增加了一個(gè)獲取接口類平痰。
在這里插入圖片描述
這個(gè)通過(guò)HttpClient自帶的getstring 方法獲取了數(shù)據(jù),然后我也把它反序列化了成了對(duì)象返回了伍纫。
下面是調(diào)用代碼
在這里插入圖片描述
注釋部分是之前的讀取本地json的部分宗雇。
下面是代碼 方便復(fù)制
using BingWallpapers.Models;
using BingWallpapers.Services;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Storage;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.Web.Http;
// https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x804 上介紹了“空白頁(yè)”項(xiàng)模板
namespace BingWallpapers
{
/// <summary>
/// 可用于自身或?qū)Ш街?Frame 內(nèi)部的空白頁(yè)。
/// </summary>
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
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);
WallpaperService wallpaperService = new WallpaperService();
WallpapersData wallPaperModel = await wallpaperService.GetWallparper(1, 8);
//通過(guò)重新組裝成集合給GridView
foreach (var item in wallPaperModel.images)
{
picModels.Add(new WallpapersDetail()
{
Title = item.copyright,
Source = "https://www.bing.com" + item.url
});
}
GV.ItemsSource = picModels;
}
/// <summary>
/// 要保存的圖片對(duì)象
/// </summary>
private WallpapersDetail wallpapers;
private void MenuFlyout_Opening(object sender, object e)
{
var menuFlyout = sender as MenuFlyout;
var gridViewItem = menuFlyout.Target as GridViewItem;
wallpapers = gridViewItem.Content as WallpapersDetail;
}
private async void Save_Click(object sender, RoutedEventArgs e)
{
Uri uri2 = new Uri(wallpapers.Source);
var httpClientPicData = new HttpClient();
var resBuffer = await httpClientPicData.GetBufferAsync(uri2);
StorageFolder destinationFolder = await KnownFolders.PicturesLibrary.CreateFolderAsync("BingWallpapers", CreationCollisionOption.OpenIfExists);
var destinationFile = await destinationFolder.CreateFileAsync("BingWallparpers"+DateTime.Now.Ticks + ".jpg", CreationCollisionOption.ReplaceExisting);
await FileIO.WriteBufferAsync(destinationFile, resBuffer);
}
}
}
在這里插入圖片描述
xaml代碼
增加了右鍵菜單莹规,一個(gè)右擊事件赔蒲,一個(gè)保存點(diǎn)擊事件,如下圖良漱。
在右擊菜單里我獲取了右擊的Gridview的item獲取了圖片的Url舞虱,然后將對(duì)象保存了wallpaper對(duì)象里,
點(diǎn)擊保存事件時(shí)母市,其實(shí)調(diào)用了一次網(wǎng)絡(luò)請(qǐng)求矾兜,因?yàn)橹朗菆D片,所以直接使用獲取buffer窒篱。最后將buffer寫入了創(chuàng)建的圖片里焕刮。
在這里插入圖片描述
<Page
x:Class="BingWallpapers.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:BingWallpapers"
xmlns:data="using:BingWallpapers.Models"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Loaded="Page_Loaded"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid>
<GridView x:Name="GV">
<GridView.ItemTemplate>
<DataTemplate x:DataType="data:WallpapersDetail">
<StackPanel>
<TextBlock Text="{x:Bind Title}"/>
<Image Source="{x:Bind Source}"/>
</StackPanel>
</DataTemplate>
</GridView.ItemTemplate>
<GridView.ContextFlyout>
<MenuFlyout Opening="MenuFlyout_Opening">
<MenuFlyoutItem Text="保存"
x:Name="Save"
Click="Save_Click"/>
</MenuFlyout>
</GridView.ContextFlyout>
</GridView>
</Grid>
</Page>
在使用圖片庫(kù)時(shí)記得在下圖里勾選圖片庫(kù)。
在這里插入圖片描述
下面是運(yùn)行之后的效果因?yàn)橛玫氖?809的sdk貌似自帶了半透明的效果
image.png
代碼還是上篇文章的墙杯,不過(guò)是在online分支,之前在csdn寫的括荡,但是那個(gè)編輯器不太好用搞了一半高镐,就換簡(jiǎn)書(shū)的了。
雖然代碼簡(jiǎn)單還是貼上來(lái)吧https://github.com/zgj1995/BingWallpapers