win8 Downlod mp3
前台代码:
<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")); }); }
} }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构