WP8.1开发:保存必应壁纸

终于写出了有实际作用的第一个软件~~

本来以为WP没有我们学校的掌上图书馆,于是想自己从实例开始学习WP开发,不过后来搜索资源时发现WP版的吉林大学掌上图书馆已经有了,是10级软件学院张文彬师兄写的。好吧,反正我都开始写了,闹着玩也是可以的。。。(附:安卓版 By 10级贾彬,超星移动图书馆(安卓版、iPhone版自己搜一下吧,我断网了))

第一个功能就是把必应每日图片作为程序背景,并且允许保存壁纸。

前台界面是类似这样子的:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67

<Page
    x:Class="App3.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App3"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <Grid>
        <!--枢轴控件《深入浅出windows phone 8.1 应用开发》P69-->
        <Pivot Title="掌上图书馆" x:Name="myPivot" >
            <!--使用必应壁纸做背景-->
            <Pivot.Background>
                <ImageBrush Opacity="{Binding ElementName=slider,Path=Value}"><!--数据绑定-->
                    <ImageBrush.ImageSource><!--P72-->
                        <BitmapImage x:Name="background"></BitmapImage>
                    </ImageBrush.ImageSource>
                </ImageBrush>
            </Pivot.Background>
            <!--第一页-->
            <PivotItem Header="检索">
                <ScrollViewer><!--内容-->
                </ScrollViewer>
            </PivotItem>
            <PivotItem Header="借阅">
                <ScrollViewer>
                    <!--内容-->
                </ScrollViewer>
            </PivotItem>
        </Pivot>
        <!--http://appserver.m.bing.net/BackgroundImageService/TodayImageService.svc/GetTodayImage?dateOffset=0&urlEncodeHeaders=true&osName=windowsPhone&osVersion=8.10&orientation=480x800&deviceName=WP8&mkt=en-US-->

<!--透明度设置面板-->
        <StackPanel Background="Aqua" Opacity="0.5" Height="200" VerticalAlignment="Top" x:Name="sliderPanel">
            <StackPanel.RenderTransform>
                <TranslateTransform x:Name="topTransform" Y="-240"></TranslateTransform>
            </StackPanel.RenderTransform>
            <TextBlock  Text="背景不透明度:" FontSize="30" Foreground="Black"/>
            <Slider x:Name="slider" Value="0.6" Minimum="0" Maximum="1" StepFrequency="0.1"/>
            <StackPanel Orientation="Horizontal">
                <TextBlock FontSize="25" Foreground="Black">当前值:</TextBlock>
                <TextBlock FontSize="25" Foreground="Black" Text="{Binding ElementName=slider,Path=Value}"/>
            </StackPanel>
        </StackPanel>
    </Grid>
<!--动画:P509-->
    <Page.Resources>
        <Storyboard x:Name="showSlider"><!--这个x:Name属性必须给出-->
            <DoubleAnimation Storyboard.TargetName="topTransform" Storyboard.TargetProperty="Y" From="-200" To="0" Duration="0:0:0.3"></DoubleAnimation>
        </Storyboard>
    </Page.Resources>
<!--菜单栏按钮:P80-->
    <Page.BottomAppBar>
        <CommandBar Opacity="0.5">
            <CommandBar.PrimaryCommands>
                <AppBarButton Icon="SaveLocal"  Label="保存壁纸" Click="saveWallpaper" IsEnabled="False" x:Name="saveButton"/>
                <AppBarButton Icon="Setting" Label="设置" Click="gotoSetting"/>
            </CommandBar.PrimaryCommands>
            <CommandBar.SecondaryCommands>
                <AppBarButton Label="透明度" Click="AppBarButton_Click_showSlider"/>
            </CommandBar.SecondaryCommands>
        </CommandBar>
    </Page.BottomAppBar>
</Page>
必应壁纸

后台代码是这样实现的:

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Storage;
using Windows.Storage.Streams;
using Windows.System;
using Windows.UI.Popups;
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;
//  http://go.microsoft.com/fwlink/?LinkId=391641 
//using System.Runtime.InteropServices.WindowsRuntime;//P164

namespace App3
{
    /// <summary>
    ///  Frame 
    /// </summary>
    public sealed partial class MainPage : Page
    {
        //
        private string TodayPictureUri = "http://appserver.m.bing.net/BackgroundImageService/TodayImageService.svc/GetTodayImage?dateOffset=0&urlEncodeHeaders=true&osName=windowsPhone&osVersion=8.10&orientation=480x800&deviceName=WP8&mkt=zh-CN";
        
        public MainPage()
        {
            this.InitializeComponent();
            //
            this.NavigationCacheMode = NavigationCacheMode.Required;
            sliderPanel.PointerExited += sliderPanel_PointerExited;
            
            //
            background.UriSource = new Uri(TodayPictureUri);
            saveButton.IsEnabled = true;
            
        }
        //
        void sliderPanel_PointerExited(object sender,PointerRoutedEventArgs e) {
            topTransform.Y = -240;
        }
        /// <summary>
        ///  Frame 
        /// </summary>
        /// <param name="e">访
        /// </param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            // TODO: 

            // TODO: 
            // 退:
            // Windows.Phone.UI.Input.HardwareButtons.BackPressed 
            // 使 NavigationHelper
            // 

            
        }

        //
        private void AppBarButton_Click_showSlider(object sender, RoutedEventArgs e)
        {
            showSlider.Begin();
        }
        //:
        private async void saveWallpaper(object sender, RoutedEventArgs e)
        {
            List<Byte> allBytes = new List<byte>();
            using (var response = await HttpWebRequest.Create(TodayPictureUri).GetResponseAsync())
            {
                using (Stream responseStream = response.GetResponseStream())
                {
                    byte[] buffer = new byte[4000];
                    int bytesRead = 0;
                    while ((bytesRead = await responseStream.ReadAsync(buffer, 0, 4000)) > 0)
                    {
                        allBytes.AddRange(buffer.Take(bytesRead));
                    }
                }
            }
            /*var file = await ApplicationData.Current.LocalFolder.CreateFileAsync(//ApplicationData只能保存在应用的私有文件存储中
                       "bingPicture" + DateTime.Now.Ticks + ".jpg", CreationCollisionOption.ReplaceExisting);
            await FileIO.WriteBytesAsync(file, allBytes.ToArray());
            */
            var file1 = await KnownFolders.SavedPictures.CreateFileAsync("jlulib_bingWallpaper_" + DateTime.Today.Ticks + ".jpg", CreationCollisionOption.ReplaceExisting);
            await FileIO.WriteBytesAsync(file1, allBytes.ToArray());
            //http://msdn.microsoft.com/zh-cn/library/windows/apps/xaml/dn639127.aspx KnownFolders Windows Phone 访Windows Phone 
          
            ContentDialog dialog = new ContentDialog()
            {//http://www.bcmeng.com/contentdialog/  Windows Phone 访Windows Phone 
                Title = "保存成功", //
                Content = "壁纸已保存至""",//
                FullSizeDesired=false,  //
                PrimaryButtonText = "知道了",//
                //SecondaryButtonText = "No, Dont!"
            };
            await dialog.ShowAsync();
            //dialog.SecondaryButtonClick += dialog_SecondaryButtonClick;//
            //dialog.PrimaryButtonClick += dialog_PrimaryButtonClick;

            //ContentDialogResult result = await dialog.ShowAsync();
            //if (result == ContentDialogResult.Primary) { } //
            //else if (result == ContentDialogResult.Secondary) { }//
        }//2014-11-10 18:00
        
    }
}
保存成功的提示对话框来自编程小梦: 保存成功的对话框
必应壁纸的调用接口: http://appserver.m.bing.net/BackgroundImageService/TodayImageService.svc/GetTodayImage?dateOffset=0&urlEncodeHeaders=true&osName=windowsPhone&osVersion=8.10&orientation=480x800&deviceName=WP8&mkt=en-US
参考资料:

Reader Echoes

10 comments

  • #1144鲜活
    很不错的哇 不过win8不好用呀 好多bug
  • #1143Tammy
    吊哦!! 大兵
  • #1142极品飞鸽
    感觉WP系统要死不死的
  • #1141Phnomi
    可爱 可爱 吊哦!!
  • #1138c
    呲牙 围观码神! 我貌似从XP以后就没怎么折腾过Windows系统了……
  • #1136未知路
    [/愤怒] 沙发没拉!。博主你的小仓鼠哪来的啊?
    • #1137Youth.霖
      回复 @未知路你看一下页面源代码就知道了,就是辣个网站,我不记得域名。。。 我好像发不出邮件了,有评论回复通知吗(我猜没有
    • #1139未知路
      回复 @Youth.霖真的没有通知啊。。。 坏笑
    • #1140Youth.霖
      回复 @未知路好久都不能发邮件了,今天提交支持单竟然叫我装插件。。。 [/愤怒] 于是装了WP-Smtp,原先没用,然后换163的账号好像有用了 发呆
  • #1135恋羽
    不错,不错 可爱 可爱

表情

评论提交后需经审核才会显示。