后台代码:

  1. public partial class MainPage : PhoneApplicationPage
  2. {
  3. private long siz;
  4. private long speed;
  5. private Stopwatch sw;
  6. private Stopwatch sw1;
  7.  
  8. public MainPage()
  9. {
  10. InitializeComponent();
  11. }
  12.  
  13. private void button1_Click(object sender, RoutedEventArgs e)
  14. {
  15. testspeed();
  16. }
  17.  
  18. public void testspeed()
  19. {
  20. WebClient client = new WebClient();
  21. progressBar1.Value = 0.0;
  22. textBox2.Text = "0 %";
  23. client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(this.webClient_DownloadStringCompleted);
  24. client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(this.webClient_DownloadProgressChanged);
  25. sw = Stopwatch.StartNew();//用来记录总的下载时间
  26. sw1 = Stopwatch.StartNew();//用来记录下载过程中的时间片,用于计算临时速度
  27. client.DownloadStringAsync(new Uri("http://dl_dir.qq.com/qqfile/qq/QQ2011/QQ2011.exe"));
  28. }
  29. //下载过程事件
  30. public void webClient_DownloadProgressChanged(object s, DownloadProgressChangedEventArgs e)
  31. {
  32. textBox2.Text = e.ProgressPercentage.ToString() + " %";
  33. sw1.Stop();
  34. long num = e.BytesReceived / 1024;
  35. if (sw1.Elapsed.Seconds != 0)
  36. {
  37. speed = num / ((long)sw1.Elapsed.Seconds);
  38. }
  39. textBlock4.Text = this.speed + " KB/s";
  40. progressBar1.Value = e.ProgressPercentage;
  41. siz = e.TotalBytesToReceive;
  42. textBlock3.Text = siz/1024/1024 + "MB";
  43. sw1.Start();
  44. }
  45. //下载完成事件
  46. public void webClient_DownloadStringCompleted(object s, DownloadStringCompletedEventArgs e)
  47. {
  48. sw.Stop();
  49. siz = siz / 1024;
  50. long num = siz / ((long)sw.Elapsed.Seconds);
  51. sw.Reset();
  52. textBox1.Text = "下载完成!";
  53. textBlock1.Text = num + " KB/s";
  54. }
  55. }
  56. }

XAML 设计代码:

  1. <phone:PhoneApplicationPage
  2. x:Class="DownLoad.MainPage"
  3. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5. xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
  6. xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
  7. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  8. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  9. mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
  10. FontFamily="{StaticResource PhoneFontFamilyNormal}"
  11. FontSize="{StaticResource PhoneFontSizeNormal}"
  12. Foreground="{StaticResource PhoneForegroundBrush}"
  13. SupportedOrientations="Portrait" Orientation="Portrait"
  14. shell:SystemTray.IsVisible="True">
  15. <Grid x:Name="LayoutRoot" Background="Transparent">
  16. <Grid.RowDefinitions>
  17. <RowDefinition Height="Auto"/>
  18. <RowDefinition Height="*"/>
  19. </Grid.RowDefinitions>
  20.  
  21. <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
  22. <TextBlock x:Name="ApplicationTitle" Text="WP7开发者" Style="{StaticResource PhoneTextNormalStyle}"/>
  23. <TextBlock x:Name="PageTitle" Text="下载进度" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
  24. </StackPanel>
  25.  
  26. <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
  27. <Grid.RowDefinitions>
  28. <RowDefinition Height="114*"/>
  29. <RowDefinition Height="493*"/>
  30. </Grid.RowDefinitions>
  31. <ProgressBar Foreground="Green" Height="30" HorizontalAlignment="Left" Margin="9,79,0,0" Name="progressBar1" VerticalAlignment="Top" Width="377" Value="0" Maximum="100" FontSize="72" BorderThickness="0"/>
  32. <TextBlock Height="53" Margin="12,20,-16,0" Name="textBox1" Text="正在下载文件 . . ." VerticalAlignment="Top" Width="460"/>
  33. <TextBlock Margin="6,24,287,427" Name="result" Text="下载的速度" Grid.Row="1"/>
  34. <TextBlock Foreground="White" Height="59" HorizontalAlignment="Left" Margin="203,13,0,0" Name="textBox2" Text="" VerticalAlignment="Top" Width="123"/>
  35. <Button Content="开始下载" Height="72" HorizontalAlignment="Left" Margin="96,244,0,0" Name="button1" VerticalAlignment="Top" Width="199" Click="button1_Click" Grid.Row="1"/>
  36. <TextBlock Height="47" Margin="6,101,254,0" Name="finalresult" Text="平均的速度" VerticalAlignment="Top" Grid.Row="1"/>
  37. <TextBlock Height="47" HorizontalAlignment="Left" Margin="132,89,0,0" Name="textBlock1" Text="" VerticalAlignment="Top" Width="156" Grid.Row="1"/>
  38. <TextBlock Height="47" HorizontalAlignment="Left" Margin="6,166,0,0" Name="textBlock2" Text="文件的大小" VerticalAlignment="Top" Width="127" Grid.Row="1"/>
  39. <TextBlock Height="47" HorizontalAlignment="Right" Margin="0,166,168,0" Name="textBlock3" Text="" VerticalAlignment="Top" Grid.Row="1" Width="156"/>
  40. <TextBlock Height="47" HorizontalAlignment="Right" Margin="0,19,168,0" Name="textBlock4" Text="" VerticalAlignment="Top" Grid.Row="1" Width="156"/>
  41. </Grid>
  42. </Grid>
  43. </phone:PhoneApplicationPage>

运行结果图:

    

Windows Phone 8 下载文件进度的更多相关文章

  1. C#获取windows 10的下载文件夹路径

    Windows没有为“下载”文件夹定义CSIDL,并且通过Environment.SpecialFolder枚举无法使用它. 但是,新的Vista 知名文件夹 API确实使用ID定义它FOLDERID ...

  2. 使用 new XMLHttpRequest() 制作下载文件进度条

    mui 进度控件使用方法: 检查当前容器(container控件)自身是否包含.mui-progressbar类: 当前容器包含.mui-progressbar类,则以当前容器为目标控件,直接显示进度 ...

  3. python实现socket上传下载文件-进度条显示

    在python的socket编程中,可以实现上传下载文件,并且在下载的时候,显示进度条,具体的流程如下图所示: 1. 服务器端代码如下: [root@python 519]# cat server.p ...

  4. 阿里云服务器(Windows)如何下载文件

    背景:公司只有我一个技术,在我之前还有一个老技术,属于兼职状态,为了尽快熟悉公司网站及app项目情况,我联系了老技术,请他尽快将代码发给我,他说代码文件过大,问我能不能连上服务器下载.百度了很多,都不 ...

  5. 使用tqdm实现下载文件进度条

    1.获取下载链接 下载链接为:http://fastsoft.onlinedown.net/down/Fcloudmusicsetup2.5.5.197764.exe 2.使用tqdm实现 2.1.从 ...

  6. linux上很方便的上传下载文件工具rz和sz使用介绍

    简单说就是,可以很方便地用这两个sz/rz工具,实现Linux下和Windows之间的文件传输(发送和接收),速度大概为10KB/s,适合中小文件.rz/sz 通过Zmodem协议传输数据   一般来 ...

  7. 在linux命令行利用SecureCRT上传下载文件

    一般来说,linux服务器大多是通过ssh客户端来进行远程的登陆和管理的,使用ssh登陆linux主机以后,如何能够快速的和本地机器进行文件的交互呢,也就是上传和下载文件到服务器和本地?与ssh有关的 ...

  8. C# Winform下载文件并显示进度条

    private void btnDown_Click(object sender, EventArgs e) { DownloadFile("http://localhost:1928/We ...

  9. WPF多线程下载文件,有进度条

    //打开对话框选择文件         private void OpenDialogBox_Click(object sender, RoutedEventArgs e)         {     ...

随机推荐

  1. 数据库 SQL Server 到 MySQL 迁移方法总结

    最近接手一起老项目数据库 SQL Server 到 MySQL 的迁移.因此迁移前进行了一些调查和总结.下面是一些 SQL Server 到 MySQL 的迁移方法. 1. 使用 SQLyog 迁移 ...

  2. SQL中JOIN 的用法

    关于sql语句中的连接(join)关键字,是较为常用而又不太容易理解的关键字,下面这个例子给出了一个简单的解释 --建表table1,table2:create table table1(id int ...

  3. FTP定时批量下载文件(SHELL脚本及使用方法 )

    1. 脚本实例 将以下脚本保存为 getftp.sh #!/bin/bash  datesign=`date -d -95day +%Y%m%d` ftp -nv 12.2.2.28 << ...

  4. win7 telnet

    一.telnet连接 1.linux linux下可以通过net stat 查看22端口来确认端口是否开放. 然后在cmd中可通过telnet 10.0.200.151 22来连接. 2.win7 w ...

  5. MarkDown插入图片

    MarkDown插入图片的语法 ·编辑器:MacDown 比如博客园的Logo,URL是 http://static.cnblogs.com/images/logo_small.gif 在要插入图片的 ...

  6. nodejs——网络编程模块

    net模块提供了一个异步网络包装器,用于TCP网络编程,它包含了创建服务器和客户端的方法.dgram模块用于UDP网络编程. 参考链接:https://nodejs.org/api/net.html, ...

  7. R语言学习笔记(一)

    1.不同的行业对数据集(即表格)的行和列称谓不同,统计学家称其为观测(observation)和变量(variable): 2.R语言存储数据的结构: ①向量:类似于C语言里的一位数组,执行组合功能的 ...

  8. hdu-5492 Find a path(dp)

    题目链接: Find a path Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  9. 网格弹簧质点系统模拟(Spring-Mass System by Euler Integration)

    弹簧质点模型是利用牛顿运动定律来模拟物体变形的方法.如下图所示,该模型是一个由m×n个虚拟质点组成的网格,质点之间用无质量的.自然长度不为零的弹簧连接.其连接关系有以下三种: 1.连接质点[i, j] ...

  10. Linux混杂设备驱动学习

    Linux混杂设备是字符设备的一类,主要是混杂设备拥有相同的主设备号(10),但是次设备号是不同的.所有的混杂设备行程一个链表,对设备访问时内核更据次设备号查找到相应的混杂设备. 混杂设备用struc ...