前段时间写了使用必应壁纸的接口,只是通过本地的json文本演示了壁纸展示,没有涉及网络获取,这回这篇文章修改了数据获取部分,换成了在线获取,项目的结构和上次的没太大的区别,所以大家可以先看看上篇文章,然后再看这篇文章。
如下图,增加了一个获取接口类。
这个通过HttpClient自带的getstring 方法获取了数据,然后我也把它反序列化了成了对象返回了。
下面是调用代码
注释部分是之前的读取本地json的部分。
下面是代码 方便复制
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 上介绍了“空白页”项模板
namespace BingWallpapers
{
/// <summary>
/// 可用于自身或导航至 Frame 内部的空白页。
/// </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);
//通过重新组装成集合给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>
/// 要保存的图片对象
/// </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代码
增加了右键菜单,一个右击事件,一个保存点击事件,如下图。
在右击菜单里我获取了右击的Gridview的item获取了图片的Url,然后将对象保存了wallpaper对象里,
点击保存事件时,其实调用了一次网络请求,因为知道是图片,所以直接使用获取buffer。最后将buffer写入了创建的图片里。
<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>
在使用图片库时记得在下图里勾选图片库。
下面是运行之后的效果因为用的是1809的sdk貌似自带了半透明的效果
代码还是上篇文章的,不过是在online分支,之前在csdn写的,但是那个编辑器不太好用搞了一半,就换简书的了。
虽然代码简单还是贴上来吧https://github.com/zgj1995/BingWallpapers