美文网首页
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使用必应每日壁纸(一)

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