win8mp3下载

前台代码:

<Page.Resources>
        <Style TargetType="TextBlock">
            <Setter Property="FontSize" Value="27"/>
            <Setter Property="FontFamily" Value="宋体"/>
        </Style>
    </Page.Resources>


    <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">

        <Grid.RowDefinitions>
            <RowDefinition Height="auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
       
        <StackPanel Grid.Row="0" Margin="15,8" Orientation="Horizontal">
            <TextBlock Text="输入下载URI:" VerticalAlignment="Center" />
            <TextBox x:Name="txtInputUri" Width="680"/>
            <Button x:Name="btnDown" Margin="38,0,0,0" VerticalAlignment="Center" Content="开始下载" Padding="17,5,17,5" FontSize="22" Click="onDownload_Click"/>
        </StackPanel>
       
        <StackPanel Grid.Row="1" Margin="20">
            <ProgressBar x:Name="probar" Maximum="100" Minimum="0" SmallChange="1" Width="700" HorizontalAlignment="Left" Foreground="Yellow"
                       Height="30" Margin="6,21,0,35"/>
            <TextBlock x:Name="tbMsg" Margin="6,8,0,0" />
        </StackPanel>


    </Grid>
< /Page>

后台代码:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Networking.BackgroundTransfer;
using Windows.Storage;
using Windows.Storage.Pickers;
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=234238 上有介绍

namespace downLoad
{
    /// <summary>
    /// 可用于自身或导航至 Frame 内部的空白页。
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// 在此页将要在 Frame 中显示时进行调用。
        /// </summary>
        /// <param name="e">描述如何访问此页的事件数据。Parameter
        /// 属性通常用于配置页。</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private async void onDownload_Click(object sender, RoutedEventArgs e)
        {
            // 判断是否已输入下载URI 
           if (string.IsNullOrWhiteSpace(this.txtInputUri.Text)) return; 
            // 选择文件保存位置 
           FileSavePicker picker = new FileSavePicker(); 
            picker.FileTypeChoices.Add("MP3文件", new string[] { ".mp3" }); 
            StorageFile file = await picker.PickSaveFileAsync(); 
            if (file != null) 
            { 
                // 实例化BackgroundDownloader 
                BackgroundDownloader downloader = new BackgroundDownloader(); 
                DownloadOperation operation = downloader.CreateDownload(new Uri(this.txtInputUri.Text), file);
                // 进度 
                Progress<DownloadOperation> progressDown = new Progress<DownloadOperation>(this.ProgressChanged);
                // 开始下载 
                btnDown.IsEnabled = false; 
                var opresult = await operation.StartAsync().AsTask(progressDown);
                btnDown.IsEnabled = true; 
               // 判断下载结果 
                switch (opresult.Progress.Status) 
                { 
                    case BackgroundTransferStatus.Completed: 
                        tbMsg.Text = "下载已完成。"; 
                        break; 
                    case BackgroundTransferStatus.Error: 
                         tbMsg.Text = "下载过程中发生了错误。"; 
                        break; 
                    default: 
                        tbMsg.Text = "未知。"; 
                        break; 
                } 
            } 
        }

               /// <summary> 
        /// 报告进度 
        /// </summary> 
        private async void ProgressChanged(DownloadOperation op) 
        { 
            var p = op.Progress; 
            double t = p.TotalBytesToReceive;//要下载总字节数 
            double d = p.BytesReceived;//已接收字节数 
            // 计算完成百分比 
            double pc = d / t * 100; 
            await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                { 
                    this.probar.Value = pc; 
                    this.tbMsg.Text = string.Format("已下载{0}字节/共{1}字节。", d.ToString("N0"), t.ToString("N0"));
                }); 
        }

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值