WP7-文件下载进度和速度显示

本文详细介绍了如何在Windows Phone 7应用中利用WebClient类实现HTTP协议下的文件异步下载,并通过监听DownloadProgressChanged和DownloadStringCompleted事件获取下载进度和完成状态。同时,展示了如何实时更新下载速度和文件大小信息,为用户提供直观的下载体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转自:http://www.cnblogs.com/linzheng/archive/2011/11/03/2234971.html

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

<phone:PhoneApplicationPage 
    x:Class="DownLoadTest.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    shell:SystemTray.IsVisible="True">

    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        
        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
            <TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock x:Name="PageTitle" Text="下载进度显示" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
        </StackPanel>

        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <Grid.RowDefinitions>
                <RowDefinition Height="114*"/>
                <RowDefinition Height="493*"/>
            </Grid.RowDefinitions>
            <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"/>
            <TextBlock Height="53" Margin="12,20,-16,0" Name="textBox1" Text="正在下载文件 . . ." VerticalAlignment="Top" Width="460"/>
            <TextBlock Margin="6,24,287,427" Name="result" Text="下载的速度:  " Grid.Row="1"/>
            <TextBlock  Foreground="White" Height="59" HorizontalAlignment="Left" Margin="203,13,0,0" Name="textBox2" Text=""  VerticalAlignment="Top" Width="123"/>
            <Button Content="测试下载" Height="72" HorizontalAlignment="Left" Margin="96,244,0,0" Name="button1" VerticalAlignment="Top" Width="199" Click="button1_Click" Grid.Row="1"/>
            <TextBlock Height="59"  Margin="2,101,254,0" Name="finalresult" Text="平均的下载速度:  " VerticalAlignment="Top" Grid.Row="1"/>
            <TextBlock Height="57" HorizontalAlignment="Left" Margin="174,89,0,0" Name="textBlock1" Text="" VerticalAlignment="Top" Width="156" Grid.Row="1"/>
            <TextBlock Height="47" HorizontalAlignment="Left" Margin="12,166,0,0" Name="textBlock2" Text="文件的大小:" VerticalAlignment="Top" Width="127" Grid.Row="1"/>
            <TextBlock Height="47" HorizontalAlignment="Right" Margin="0,166,190,0" Name="textBlock3" Text="" VerticalAlignment="Top" Grid.Row="1" Width="121"/>
            <TextBlock Height="47" HorizontalAlignment="Right" Margin="0,19,190,0" Name="textBlock4" Text="" VerticalAlignment="Top" Grid.Row="1" Width="134"/>
        </Grid>
    </Grid>
</phone:PhoneApplicationPage>

 

using System;
using System.Net;
using System.Windows;
using Microsoft.Phone.Controls;
using System.Diagnostics;

namespace DownLoadTest
{
    public partial class MainPage : PhoneApplicationPage
    {
        private long siz;
        private long speed;
        private Stopwatch sw;
        private Stopwatch sw1;

        public MainPage()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            testspeed();
        }

        public void testspeed()
        {
            WebClient client = new WebClient();
            progressBar1.Value = 0.0;
            textBox2.Text = "0 %";
            client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(this.webClient_DownloadStringCompleted);
            client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(this.webClient_DownloadProgressChanged);
            sw = Stopwatch.StartNew();//用来记录总的下载时间
            sw1 = Stopwatch.StartNew();//用来记录下载过程中的时间片,用于计算临时速度
            client.DownloadStringAsync(new Uri("http://dl_dir.qq.com/qqfile/qq/QQ2011/QQ2011.exe"));
        }
        //下载过程事件
        public void webClient_DownloadProgressChanged(object s, DownloadProgressChangedEventArgs e)
        {
            textBox2.Text = e.ProgressPercentage.ToString() + " %";
            sw1.Stop();
            long num = e.BytesReceived / 1024;
            if (sw1.Elapsed.Seconds != 0)
            {
                speed = num / ((long)sw1.Elapsed.Seconds);
            }
            textBlock4.Text = this.speed + " KB/sec";
            progressBar1.Value = e.ProgressPercentage;
            siz = e.TotalBytesToReceive;
            textBlock3.Text = siz + "KB";
            sw1.Start();
        }
        //下载完成事件
        public void webClient_DownloadStringCompleted(object s, DownloadStringCompletedEventArgs e)
        {
            sw.Stop();
            siz = siz/1024;
            long num = siz / ((long)sw.Elapsed.Seconds);
            sw.Reset();
            textBox1.Text = "下载完成!";
            textBlock1.Text = num + " KBytes/sec";
        }
    }
}

 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值