silverlight2 下DownloadProgressChanged事件不触发?

twj031500 2009-01-08 11:44:18
这是网上摘下来的一个例子,但在我机器上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;
}
}
}
...全文
119 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
刀枪blue 2009-01-19
  • 打赏
  • 举报
回复
报什么错啊
twj031500 2009-01-17
  • 打赏
  • 举报
回复
为什么不能是绝对路径,如果想下载远程的东西岂不是不行了?
而且我现在改为相对后编译报错
请赐教?
刀枪blue 2009-01-09
  • 打赏
  • 举报
回复
wc.OpenReadAsync(new Uri(filepath.Text, UriKind.Relative));
imgsrc.SetSource(e.Result as Stream);
img.Source = imgsrc;

8,737

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧