Windows Phone 7文件下载进度和速度显示

移动开发
用http协议来下载网络上的文件,通常我们需要获取文件文件的下载进度和下载的速度来给用户等待过程的一个交代,那么在Windows Phone 7下可以使用WebClient类来实现这一功能。

用http协议来下载网络上的文件,通常我们需要获取文件文件的下载进度和下载的速度来给用户等待过程的一个交代,那么在Windows Phone 7下可以使用WebClient类来实现这一功能,HttpWebRequest类也可以用于下载网络上的文件,不过HttpWebRequest类不能够直接地获取你http请求的完成情况。

使用WebClient.DownloadProgressChanged事件来异步获取http协议下载文件的进度情况,使用WebClient.DownloadStringCompleted事件来判断文件的下载是否完成。

  1. <phone:PhoneApplicationPage   
  2.     x:Class="DownLoadTest.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.  
  16.     <Grid x:Name="LayoutRoot" Background="Transparent"> 
  17.         <Grid.RowDefinitions> 
  18.             <RowDefinition Height="Auto"/> 
  19.             <RowDefinition Height="*"/> 
  20.         </Grid.RowDefinitions> 
  21.           
  22.         <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28"> 
  23.             <TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/> 
  24.             <TextBlock x:Name="PageTitle" Text="下载进度显示" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/> 
  25.         </StackPanel> 
  26.  
  27.         <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> 
  28.             <Grid.RowDefinitions> 
  29.                 <RowDefinition Height="114*"/> 
  30.                 <RowDefinition Height="493*"/> 
  31.             </Grid.RowDefinitions> 
  32.             <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"/> 
  33.             <TextBlock Height="53" Margin="12,20,-16,0" Name="textBox1" Text="正在下载文件 . . ." VerticalAlignment="Top" Width="460"/> 
  34.             <TextBlock Margin="6,24,287,427" Name="result" Text="下载的速度:  " Grid.Row="1"/> 
  35.             <TextBlock  Foreground="White" Height="59" HorizontalAlignment="Left" Margin="203,13,0,0" Name="textBox2" Text=""  VerticalAlignment="Top" Width="123"/> 
  36.             <Button Content="测试下载" Height="72" HorizontalAlignment="Left" Margin="96,244,0,0" Name="button1" VerticalAlignment="Top" Width="199" Click="button1_Click" Grid.Row="1"/> 
  37.             <TextBlock Height="59"  Margin="2,101,254,0" Name="finalresult" Text="平均的下载速度:  " VerticalAlignment="Top" Grid.Row="1"/> 
  38.             <TextBlock Height="57" HorizontalAlignment="Left" Margin="174,89,0,0" Name="textBlock1" Text="" VerticalAlignment="Top" Width="156" Grid.Row="1"/> 
  39.             <TextBlock Height="47" HorizontalAlignment="Left" Margin="12,166,0,0" Name="textBlock2" Text="文件的大小:" VerticalAlignment="Top" Width="127" Grid.Row="1"/> 
  40.             <TextBlock Height="47" HorizontalAlignment="Right" Margin="0,166,190,0" Name="textBlock3" Text="" VerticalAlignment="Top" Grid.Row="1" Width="121"/> 
  41.             <TextBlock Height="47" HorizontalAlignment="Right" Margin="0,19,190,0" Name="textBlock4" Text="" VerticalAlignment="Top" Grid.Row="1" Width="134"/> 
  42.         </Grid> 
  43.     </Grid> 
  44. </phone:PhoneApplicationPage> 

 

 

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

 

实例截图

原文链接:http://www.cnblogs.com/linzheng/archive/2011/11/03/2234971.html

责任编辑:王晓东 来源: cnblogs
相关推荐

2009-01-11 09:52:14

Windows 7微软补丁

2011-08-30 09:05:18

Windows PhoWP7稳定

2011-02-15 09:49:31

Windows 7文件访问

2011-07-12 09:20:32

Windows 8Windows Pho

2009-12-01 09:23:57

Windows 7文件共享

2010-08-02 14:47:51

Windows PhoWindows PhoWindows Pho

2009-08-06 08:42:42

Windows 7文件管理员权限

2011-08-09 18:20:52

windows7文件夹

2011-04-19 09:49:31

市场份额webOSWindows Pho

2010-03-19 08:45:20

Windows Pho

2010-11-26 16:00:08

Windows Pho

2013-07-30 11:18:37

Windows PhoWindows Pho

2010-05-05 13:16:02

Windows PhoWindows CE

2011-04-01 13:20:40

Windows Pho应用程序

2013-06-21 10:48:18

WP7Windows Pho中英文互翻译

2010-11-04 18:11:35

UI设计SilverlightWindows Pho

2011-08-29 09:26:57

Windows 8文件界面

2009-11-25 09:21:53

Windows 7导航

2011-08-22 16:45:58

Windows PhoiOS

2011-06-07 11:35:38

Windows Pho
点赞
收藏

51CTO技术栈公众号