silverlight2 下DownloadProgressChanged事件不触发?
这是网上摘下来的一个例子,但在我机器上DownloadProgressChanged就是不会被触发,哪位兄弟姐妹们看看,不弟感激不尽了!!
xmal代码如下:
<UserControl x:Class="SilverlightApplication10.Page"
xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="1024" Height="800">
<Grid x:Name="LayoutRoot" Background="White">
<Canvas Canvas.Top="0">
<Rectangle Name="progressRectangle" Canvas.Left="20" Canvas.Top="10" Height="10" Width="0" Fill="Navy" />
<Rectangle Canvas.Top="9" Canvas.Left="19" Height="12" Width="202" StrokeThickness="1" Stroke="Black" />
<TextBlock x:Name="progressText" Canvas.Top="6" Canvas.Left="230" Text="0%" FontSize="12" />
<TextBox x:Name="filepath" Canvas.Left="20" Width="100" Canvas.Top="26" Text="http://www.it.com.cn/f/edu/079/24/IT_bz_PSmhfj_001.jpg"></TextBox>
<Button Canvas.Top="26" Canvas.Left="130" Content="下载文件" Click="Button_Click"></Button>
<Image Canvas.Left="60" x:Name="img" Canvas.Top="60" Width="1024"></Image>
</Canvas>
</Grid>
</UserControl>
C#代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Net;
using System.IO;
using System.Windows.Resources;
namespace SilverlightApplication10
{
public partial class Page : UserControl
{
public Page()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
img.Source = null;
WebClient wc = new WebClient();
if (wc.IsBusy)
{
wc.CancelAsync();
}
progressText.Text = "start";
wc.DownloadProgressChanged += new DownloadProgressChangedEventHandler(wc_DownloadProgressChanged);
wc.OpenReadCompleted += new OpenReadCompletedEventHandler(wc_OpenReadCompleted);
wc.OpenReadAsync(new Uri(filepath.Text, UriKind.Absolute));
}
void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
BitmapImage imgsrc = new BitmapImage();
progressText.Text = "finish";
//((System.Windows.Media.Imaging.BitmapImage)this.img.Source).UriSource = new Uri("http://www.it.com.cn/f/edu/079/24/IT_bz_PSmhfj_001.jpg", UriKind.Absolute);
//imgsrc.SetSource(e.Result as Stream);
//img.Source = imgsrc;
}
void wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
progressText.Text = e.ProgressPercentage.ToString() + "%";
progressRectangle.Width = (double)e.ProgressPercentage * 2;
}
}
}