美文网首页
UWP使用必应每日壁纸(一)

UWP使用必应每日壁纸(一)

作者: Gil_Zhang | 来源:发表于2018-10-24 00:00 被阅读0次

必应壁纸接口我在网上找了很多大体都是第三方经过加工,所以稳定性不太清楚能不能长久使用也不清楚。
于是乎我找了很久在https://stackoverflow.com上找到了感觉还算官方的接口。
正如下面所示,有三种格式,我采用的是json格式。
参数有四个,format,idx,n,和mkt。idx就是索引吧,我猜的应该是0到14吧,表示半个月的壁纸,从当前日期开始查,n应该是一次查询的数量,后面的mkt应该就是国家的设置,这个可以自己试试,访问json连接会返回一串json。

XML:http://www.bing.com/HPImageArchive.aspx?format=xml&idx=0&n=1&mkt=en-US

JSON:http://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=en-US

RSS:http://www.bing.com/HPImageArchive.aspx?format=rss&idx=0&n=1&mkt=en-US

Json数据如下图所示:

image

然后打开VS创建一个UWP项目命名可以随意。

image

然后在资源目录新建个json文件,为了方便我就不在线获取json了,把之前浏览器返回的json贴到这个文件里。

image

此时比较重要的一件事就是将此文件属性的生成操作更改为内容不然会报错。

image.png

这些做完以后就可以进行下面的壁纸获取操作了。在项目里新建一个Models文件夹,在文件夹里新建两个类,命名根据自己需要。一个类是为了反序列化壁纸json的,我用的是WallpapersData。

image.png
在WallpapersData类里选择编辑然后选择性粘贴,就可以将之前的壁纸json粘贴成类。 image
粘贴以后大致就图所示结构
image.png
然后另外的一个类就是用来保存处理之后的壁纸内容了。目前我就设置了两条信息。 image.png

在mainPage里写入如下的代码

<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>
    </Grid>
</Page>

然后在后台代码里写入如下代码
记得要安装Newtonsoft.json库

using BingWallpapers.Models;
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;

// 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);
            //通过重新组装成集合给GridView
            foreach (var item in wallPaperModel.images)
            {
                picModels.Add(new WallpapersDetail()
                {
                    Title = item.copyright,
                    Source = "https://www.bing.com" + item.url
                });
            }
            GV.ItemsSource = picModels;
        }
    }
}

大致就接近尾声了最终来张效果图

image.png

虽然代码简单还是贴上来吧,首次在简书写帖子多多关照https://github.com/zgj1995/BingWallpapers

相关文章

  • UWP使用必应每日壁纸(一)

    必应壁纸接口我在网上找了很多大体都是第三方经过加工,所以稳定性不太清楚能不能长久使用也不清楚。于是乎我找了很久在h...

  • UWP使用必应每日壁纸(二)

    前段时间写了使用必应壁纸的接口,只是通过本地的json文本演示了壁纸展示,没有涉及网络获取,这回这篇文章修改了数据...

  • Python下载必应今日图片

    利用Python下载必应每日壁纸,其实很简单,每日壁纸,这个网址指向了今天的必应壁纸,我们只需要把图片保存下来就好...

  • 高清壁纸网站

    高清壁纸网站地址:电脑壁纸 1.每日更新最新壁纸; 2.多种分类壁纸; 3.必应美图; 4.每日英语; 5.下载壁...

  • 必应每日壁纸

    Daily Wallpaper Bing Edition on the Mac App Store

  • 必应每日壁纸

    每日壁纸API 必应首页的壁纸比较好看,可以当作网站或者博客的背景图,但是要是静态网站或者博客每天换就有一些麻烦了...

  • Automator定时任务

    以每天定时下载必应并更换为壁纸为例 选择执行 代码看这里自动下载必应每日一图并设置为mac壁纸 - 简书之后保存然...

  • python下载每日必应壁纸

  • 高清壁纸网站都在这儿,桌面从此更精致

    1.必应壁纸 网址链接:https://bing.ioliu.cn/ 推荐指数:4.0星 优点:必应壁纸出品自微软...

  • win下自己常用软件

    以免忘记。 Listary 文件搜索 f.lux 护眼 cmder 命令行替代 ss 科学上网 每日必应 壁纸 m...

网友评论

      本文标题:UWP使用必应每日壁纸(一)

      本文链接:https://www.haomeiwen.com/subject/dxmzzftx.html