自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Windows Phone 7文件下載進(jìn)度和速度顯示

移動開發(fā)
用http協(xié)議來下載網(wǎng)絡(luò)上的文件,通常我們需要獲取文件文件的下載進(jìn)度和下載的速度來給用戶等待過程的一個交代,那么在Windows Phone 7下可以使用WebClient類來實現(xiàn)這一功能。

用http協(xié)議來下載網(wǎng)絡(luò)上的文件,通常我們需要獲取文件文件的下載進(jìn)度和下載的速度來給用戶等待過程的一個交代,那么在Windows Phone 7下可以使用WebClient類來實現(xiàn)這一功能,HttpWebRequest類也可以用于下載網(wǎng)絡(luò)上的文件,不過HttpWebRequest類不能夠直接地獲取你http請求的完成情況。

使用WebClient.DownloadProgressChanged事件來異步獲取http協(xié)議下載文件的進(jìn)度情況,使用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="下載進(jìn)度顯示" 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="文件的大?。?quot; 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

責(zé)任編輯:王曉東 來源: cnblogs
相關(guān)推薦

2009-01-11 09:52:14

Windows 7微軟補(bǔ)丁

2011-08-30 09:05:18

Windows PhoWP7穩(wěn)定

2011-02-15 09:49:31

Windows 7文件訪問

2011-07-12 09:20:32

Windows 8Windows Pho

2010-08-02 14:47:51

Windows PhoWindows PhoWindows Pho

2009-12-01 09:23:57

Windows 7文件共享

2011-08-09 18:20:52

windows7文件夾

2009-08-06 08:42:42

Windows 7文件管理員權(quán)限

2010-03-19 08:45:20

Windows Pho

2013-07-30 11:18:37

Windows PhoWindows Pho

2011-04-19 09:49:31

市場份額webOSWindows Pho

2010-11-26 16:00:08

Windows Pho

2011-08-29 09:26:57

Windows 8文件界面

2010-05-05 13:16:02

Windows PhoWindows CE

2010-11-04 18:11:35

UI設(shè)計SilverlightWindows Pho

2011-04-01 13:20:40

Windows Pho應(yīng)用程序

2013-06-21 10:48:18

WP7Windows Pho中英文互翻譯

2014-02-25 10:16:36

微軟Windows Pho

2010-12-14 18:48:49

微軟

2010-10-11 14:42:49

Windows Pho
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號