前台代碼: <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")); }); } } }