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

  使用WebClient.DownloadProgressChanged事件来异步获取http协议下载文件的进度情况;

  使用WebClient.DownloadStringCompleted事件来判断文件的下载是否完成。

     <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>
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();
            ;
            )
            {
                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/;
            long num = siz / ((long)sw.Elapsed.Seconds);
            sw.Reset();
            textBox1.Text = "下载完成!";
            textBlock1.Text = num + " KBytes/sec";
        }
    }
}

Windows Phone 之文件下载进度和速度显示的更多相关文章

  1. Python展示文件下载进度条

    前言 大家在用Python写一些小程序的时候,经常都会用到文件下载,对于一些较小的文件,大家可能不太在乎文件的下载进度,因为一会就下载完毕了. 但是当文件较大,比如下载chromedriver的时候, ...

  2. vue项目实现文件下载进度条

    平时业务中下载文件方式常见的有俩种: 第一种,直接访问服务器的文件地址,自动下载文件: 第二种 ,服务器返回blob文件流,再对文件流进行处理和下载. 一般小文件适用于第一种下载方案,不占用过多服务器 ...

  3. Winform之跨线程访问控件(在进度条上显示字体)

    此文章对于遇到必须使用线程但是没有办法在线程内操作控件的问题的处理  有很好的解决方案(个人认为的.有更好的方案欢迎交流.) 在做跨线程访问之前我们先了解下我们所做的需要达到的效果: 这个是批量的将x ...

  4. Windows Server 2012 R2在桌面上显示计算机/网络图标

    原文 Windows Server 2012 R2在桌面上显示计算机/网络图标 从Windows2012开始,微软取消了服务器桌面个性化选项,如何重新调出配置界面,可以使用微软命令调出.具体方法如下: ...

  5. HTML5效果:Canvas 实现圆形进度条并显示数字百分比

    实现效果 1.首先创建html代码 <canvas id="canvas" width="500" height="500" styl ...

  6. Windows平台下如何使用node.js显示系统盘符

    本文地址: http://www.cnblogs.com/blackmanba/articles/windows-nodejs-show-system-letter.html或者http://fork ...

  7. Android开发之文件下载,状态时显示下载进度,点击自动安装

    在进行软件升级时,需要进行文件下载,在这里实现自定义的文件下载,并在状态栏显示下载进度,下载完成后,点击触发安装. 效果如图: 用于下载文件和显示现在进度的线程类如下: [java]  view pl ...

  8. Windows Form调用R进行绘图并显示

    R软件功能非常强大,可以很好的进行各类统计,并能输出图形.下面介绍一种R语言和C#进行通信的方法,并将R绘图结果显示到WinForm UI界面上. 1 前提准备 安装R软件,需要安装32位的R软件,6 ...

  9. 基于uploadify.js实现多文件上传和上传进度条的显示

    uploadify是JQuery的一个插件,主要实现文件的异步上传功能,可以自定义文件大小限制.文件类型.是否自动上传等属性,可以显示上传的进度条.官网地址是http://www.uploadify. ...

随机推荐

  1. java 删除字符串中的特定字符

    /** * Delete any character in a given String. * @param inString the original String * @param charsTo ...

  2. Redis未授权访问缺陷让服务器沦为肉鸡

    朋友的一个项目说接到阿里云的告警,提示服务器已沦为肉鸡,网络带宽被大量占用,网站访问很慢,通过SSH远程管理服务器还频繁断开链接.朋友不知如何下手,便邀请我帮忙处理. 阿里云的安全告警邮件内容: 在没 ...

  3. PAT 1017. Queueing at Bank

    Suppose a bank has K windows open for service.  There is a yellow line in front of the windows which ...

  4. Java多线程编程的常见陷阱(转)

    Java多线程编程的常见陷阱 2009-06-16 13:48 killme2008 blogjava 字号:T | T 本文介绍了Java多线程编程中的常见陷阱,如在构造函数中启动线程,不完全的同步 ...

  5. Java三大主流框架概述(转载)

    转自:http://www.douban.com/note/320140839/ Struts.Hibernate和Spring是我们Java开发中的常用关键,他们分别针对不同的应用场景给出最合适的解 ...

  6. Redis实战之Redis + Jedis[转]

    http://blog.csdn.net/it_man/article/details/9730605 2013-08-03 11:01 1786人阅读 评论(0) 收藏 举报   目录(?)[-] ...

  7. John(博弈)

    Description Little John is playing very funny game with his  younger brother. There  is one big box ...

  8. [TypeScript] Configuring TypeScript Which Files to Compile with "Files" and "OutDir"

    This lesson shows how to configure the .tsconfig so you only compile the .ts files you want. It then ...

  9. 管理http服务的脚本

    因为老是须要又一次安装系统,重一次都要又一次设置http服务的启动脚本.麻烦,所以这一次就把脚本备份出来. httpd for Ubuntu system: nginx + php-fpm #! /b ...

  10. cocos2d-x 2.1.4学习笔记之HelloWorld分析

    下面截图是HelloWorld项目下的文件夹结构 这是用python命令生成的项目,在创建过程中默认生成多个平台的程序文件. 1.“resource”文件夹 该文件夹主要用于存放游戏中需要的图片.音频 ...