8,757
社区成员
发帖
与我相关
我的任务
分享
<UserControl x:Class="SilverlightApplication1.WebClientDownload"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc=
"http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d">
<Grid x:Name="LayoutRoot" Background="White">
<Grid.RowDefinitions>
<RowDefinition Height="0.09*"/>
<RowDefinition Height="0.17*"/>
<RowDefinition Height="0.74*"/>
</Grid.RowDefinitions>
<Canvas Canvas.Top="70" Grid.RowSpan="3">
<Rectangle x:Name="progressRectangle" Canvas.Left="96" Height="10" Width="0" Fill="Maroon" Canvas.Top="42" />
<Rectangle Canvas.Top ="42" Canvas.Left="96" Height="10" Width="200" StrokeThickness="1" Stroke="Black" />
<TextBlock x:Name="progressText" Canvas.Top="36.545" Canvas.Left="302" Text="0%" FontSize="12" />
</Canvas>
<Button HorizontalAlignment="Left" Margin="23,8,0,16" Width="67" Grid.Row="1" Content="Download"
x:Name="btnDownload" Click="btnDownload_Click"/>
<Image Margin="8.5,8.5,8,8" Grid.Row="2" x:Name="img"/>
</Grid>
</UserControl>
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 System.Windows.Media.Imaging;
using System.Windows.Resources;
namespace SilverlightApplication1
{
public partial class WebClientDownload : UserControl
{
WebClient wc = null;
public WebClientDownload()
{
InitializeComponent();
wc = new WebClient();
wc.DownloadProgressChanged += new DownloadProgressChangedEventHandler(wc_DownloadProgressChanged);
wc.OpenReadCompleted += new OpenReadCompletedEventHandler(wc_OpenReadCompleted);
}
//進度
void wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
progressText.Text = e.ProgressPercentage.ToString();
progressRectangle.Width = (double)e.ProgressPercentage*2;
}
//下載完成,載入圖片
void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
if (e.Error != null) { return; }
BitmapImage bi = new BitmapImage();
bi.SetSource(e.Result);
img.Source = bi;
}
private void btnDownload_Click(object sender, RoutedEventArgs e)
{
//檔案放在ClientBin的上層
wc.OpenReadAsync(new Uri("../YY.jpg", UriKind.Relative));
}
}
}
