WP7下载文件测试

WP7下载文件测试

WP7下载文件测试:

Xaml中的代码:  
  
<phone:PhoneApplicationPage 
    x:Class="WP7下载文件测试.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">

    <!--LayoutRoot 是包含所有页面内容的根网格-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!--TitlePanel 包含应用程序的名称和页标题-->
        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
            <TextBlock x:Name="ApplicationTitle" Text="网络大文件下载测试 " Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock x:Name="PageTitle" Text="下载测试" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
        </StackPanel>

        <!--ContentPanel - 在此处放置其他内容-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <Button Content="开始下载" Height="93" HorizontalAlignment="Left" Margin="274,39,0,0" Name="button1" VerticalAlignment="Top" Width="146" Click="button1_Click" />
            <TextBox Height="72" HorizontalAlignment="Left" Margin="117,142,0,0" Name="textBox1" Text="TextBox" VerticalAlignment="Top" Width="317" />
            <TextBox Height="74" HorizontalAlignment="Left" Margin="117,232,0,0" Name="textBox2" Text="TextBox" VerticalAlignment="Top" Width="317" />
            <TextBlock Height="30" HorizontalAlignment="Left" Margin="37,163,0,0" Name="textBlock1" Text="下载状态" VerticalAlignment="Top" />
            <TextBlock Height="30" HorizontalAlignment="Left" Margin="43,255,0,0" Name="textBlock2" Text="下载进度" VerticalAlignment="Top" />
            <TextBlock Height="30" HorizontalAlignment="Left" Margin="47,337,0,0" Name="textBlock3" Text="总大小" VerticalAlignment="Top" />
            <ProgressBar Height="85" HorizontalAlignment="Left" Margin="43,449,0,0" Name="progressBar1" VerticalAlignment="Top" Width="370" Foreground="#FFEF6A18" Background="WhiteSmoke" />
            <TextBlock Height="30" HorizontalAlignment="Left" Margin="47,373,0,0" Name="textBlock4" Text="下载速度" VerticalAlignment="Top" Width="293" />
        </Grid>
    </Grid>
 
    <!--演示 ApplicationBar 用法的示例代码-->
    <!--<phone:PhoneApplicationPage.ApplicationBar>
        <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
            <shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="按钮 1"/>
            <shell:ApplicationBarIconButton IconUri="/Images/appbar_button2.png" Text="按钮 2"/>
            <shell:ApplicationBar.MenuItems>
                <shell:ApplicationBarMenuItem Text="菜单项 1"/>
                <shell:ApplicationBarMenuItem Text="菜单项 2"/>
            </shell:ApplicationBar.MenuItems>
        </shell:ApplicationBar>
    </phone:PhoneApplicationPage.ApplicationBar>-->

</phone:PhoneApplicationPage>

后台代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Diagnostics;

namespace WP7下载文件测试
{
    public partial class MainPage : PhoneApplicationPage
    {
        // 构造函数
        public MainPage()
        {
            InitializeComponent();
        }
        private long size; private long speed;
        private Stopwatch stopwatch1;
        private Stopwatch stopwatch2;
        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);
            stopwatch1 = Stopwatch.StartNew();//用来记录总的下载时间      
            stopwatch2 = Stopwatch.StartNew();//用来记录下载过程中的时间片,用于计算临时速度  
            client.DownloadStringAsync(new Uri("http://du.xmanyao.info/COFFdD0xMzM0NzMwNzcwJmk9MTIzLjIzMi4xMDkuMyZ1PVNvbmdzL3YxL2ZhaW50UUMvNTYvYzgyNjI4ZTVjZmEzMGNiZDk1MzAwOGE4OTIwZTI4NTYubXAzJm09ZTgyNjA3YmJjN2ZiNWM1NmYzNjAzZDA1MzE1ZDE1OGQmdj1kb3duJm49yMvIy7CuJnM9s8LeyNG4JnA9bg==.mp3"));
        }
        //下载过程事件     
        public void webClient_DownloadProgressChanged(object s, DownloadProgressChangedEventArgs e)
        {
            textBox2.Text = e.ProgressPercentage.ToString() + " %"; stopwatch2.Stop();
            long num = e.BytesReceived / 1024;
            if (stopwatch2.Elapsed.Seconds != 0)
            {
                speed = num / ((long)stopwatch2.Elapsed.Seconds);
            } textBlock4.Text = this.speed + " KB/sec";
            progressBar1.Value = e.ProgressPercentage;
            size = e.TotalBytesToReceive;
            textBlock3.Text = size + "KB";
            stopwatch2.Start();
        }
        //下载完成事件     
        public void webClient_DownloadStringCompleted(object s, DownloadStringCompletedEventArgs e)
        {
            stopwatch1.Stop();
            size = size / 1024;
            long num = size / ((long)stopwatch1.Elapsed.Seconds);
            stopwatch1.Reset();
            textBox1.Text = "下载完成!";
            textBlock1.Text = num + " KBytes/sec";
        }

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


    }

}

显示图片:


WP7下载文件测试
https://www.dearcloud.cn/2012/04/18/20200310-cnblogs-old-posts/20120418-WP7下载文件测试/
作者
宋兴柱
发布于
2012年4月18日
许可协议