最近笔者几篇文章介绍了改下载文件的文章. 关联文章的地址
前台代码:
<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"));
});
}
}
}
文章结束给大家分享下程序员的一些笑话语录:
真正的程序员喜欢兼卖爆米花,他们利用CPU散发出的热量做爆米花,可以根据米花爆裂的速度听出正在运行什么程序。