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

简介:

用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.     }  

 

 

本文转自linzheng 51CTO博客,原文链接:http://blog.51cto.com/linzheng/1078467


相关文章
|
Android开发 iOS开发 Windows
Windows Phone 寿终正寝了,这些经典机型你还记得吗?
不久前,随着最后一家WP手机厂商惠普宣布取消今后Windows Phone的研发计划,以及微软官方声明对WP8.1系统今后所有升级维护的终止,WP手机,作为曾经和安卓手机、苹果手机并驾齐驱的三大智能手机之一,正式寿终正寝。
1984 0
Windows Phone 寿终正寝了,这些经典机型你还记得吗?
|
XML 开发框架 前端开发
Windows Phone快速入门需掌握哪些能力
在此之前,先普及下Windows Phone的概念和开发工具的介绍。 Windows Phone是微软公司开发的手机操作系统,它将微软旗下的Xbox Live游戏、Xbox Music音乐与独特的视频体验集成至手机中。2012年6月21日,微软正式发布Windows Phone 8,采用和Windows 8相同的Windows NT内核,同时也针对市场的Windows Phone 7.5发布Windows Phone 7.8。
385 0
Windows Phone快速入门需掌握哪些能力
|
移动开发 Android开发 开发者
Windows Phone 8.1 新功能汇总 开发者预览版开放下载
在Build 2014大会上,微软正式发布了传闻已久的Windows Phone 8.1系统,所有的Windows Phone 8手机都可以升级,微软这次可谓是十分厚道。虽然并非迭代升级,但WP 8.1还是拥有很多重大更新,对于微软进一步完善移动平台拥有积极的意义。下面,就一起来了解一下WP 8.1的主要新特性。
435 0
Windows Phone 8.1 新功能汇总 开发者预览版开放下载
|
编解码 前端开发 JavaScript
Windows Phone 下开发 LBS 应用
基于位置的服务(Location Based Service,LBS),它是通过电信移动运营商的无线电通讯网络(如GSM网、CDMA网)或外部定位方式(如GPS)获取移动终端用户的位置信息(地理坐标,或大地坐标),在GIS(Geographic Information System,地理信息系统)平台的支持下,为用户提供相应服务的一种增值业务。
452 0
|
Windows 数据安全/隐私保护 C#