两部分:
第一部分:无具体进度,只显示忙
BusyIndicator 包裹整个页面,如下
<controlsToolkit:BusyIndicator x:Name="busyIndicator" IsBusy="False">
<Grid x:Name="LayoutRoot" Margin="2" Width="960" Background=" #ECF6F8" MinHeight="450" >
<controlsToolkit:BusyIndicator x:Name="busyIndicator" IsBusy="False">
<Grid x:Name="LayoutRoot" Margin="2" Width="960" Background=" #ECF6F8" MinHeight="450" >
第二部分:有具体进度 27%
2009-12-29 10:48
ProgressBar控件可以分为两种类型:
第一种进度条不会显示出实际完成的比例,只是呈现一个动画外观来表示作业仍在进行中。欲做此类型的进
度条,需把IsIndeterminate属性设置为True.
第二种则是根据赋给value属性的值来显示出完成进度。欲做此类型的进度条,可将IsIndeterminate属性设置成false,然后使用Minimum和Maximum属性来设置范围。Minimum默认是0,Maximu默认是100;
后台代码
代码
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;
namespace SilverlightApplicationforCH9
{
public partial class CH9_DemoForm016 : UserControl
{
private const double IMAGE_WIDTH = 640;
private const double IMAGE_HEIGHT = 480;
private BitmapImage _bitmapImage;
public CH9_DemoForm016()
{
InitializeComponent();
txtUrl.Text = GetUrl.GetAbsoluteUrl("Photo/Large/LargePhoto.png");
// 将进度列隐藏起来。
ProgressBar.Visibility = Visibility.Collapsed;
// 将文字 "下载中" 隐藏起来。
Label.Visibility = Visibility.Collapsed;
// 指派“下载照片”按钮的 Click 事件处理常式。
btnLoad.Click += new RoutedEventHandler(btnLoad_Click);
}
void btnLoad_Click(object sender, RoutedEventArgs e)
{
// 将进度列显示出来。
ProgressBar.Visibility = Visibility.Visible;
// 将文字 "下载中" 显示出来。
Label.Visibility = Visibility.Visible;
// 将进度列的 Value 属性值重新设定成 0 。
ProgressBar.Value = 0;
Label.Text = "下载中...";
ImageCanvas.Children.Clear();
if (_bitmapImage != null)
{
_bitmapImage.DownloadProgress -= new EventHandler<DownloadProgressEventArgs>(bitmapImage_DownloadProgress);
}
// 建立一个 BitmapImage 物件。
_bitmapImage = new BitmapImage();
// 指派 BitmapImage 物件的 DownloadProgress 事件处理常式。
_bitmapImage.DownloadProgress += new EventHandler<DownloadProgressEventArgs>(bitmapImage_DownloadProgress);
// 设定 BitmapImage 物件的照片来源。
_bitmapImage.UriSource = new Uri(txtUrl.Text, UriKind.Absolute);
// 动态建立一个 Image 控制项。
Image newImage = new Image() { Source = _bitmapImage };
// 设定 Image 控制项的属性。
newImage.Stretch = Stretch.UniformToFill;
newImage.Width = IMAGE_WIDTH;
newImage.Height = IMAGE_HEIGHT;
// 将动态建立的 Image 控制项加入画布中。
ImageCanvas.Children.Add(newImage);
}
void bitmapImage_DownloadProgress(object sender, DownloadProgressEventArgs e)
{
// 设定进度列的 Value 属性以便更新进度列。
ProgressBar.Value = e.Progress;
Label.Text = "下载中:" + e.Progress + "%";
if (e.Progress == 100)
{
ProgressBar.Visibility = Visibility.Collapsed;
Label.Visibility = Visibility.Collapsed;
}
}
}
}
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;
namespace SilverlightApplicationforCH9
{
public partial class CH9_DemoForm016 : UserControl
{
private const double IMAGE_WIDTH = 640;
private const double IMAGE_HEIGHT = 480;
private BitmapImage _bitmapImage;
public CH9_DemoForm016()
{
InitializeComponent();
txtUrl.Text = GetUrl.GetAbsoluteUrl("Photo/Large/LargePhoto.png");
// 将进度列隐藏起来。
ProgressBar.Visibility = Visibility.Collapsed;
// 将文字 "下载中" 隐藏起来。
Label.Visibility = Visibility.Collapsed;
// 指派“下载照片”按钮的 Click 事件处理常式。
btnLoad.Click += new RoutedEventHandler(btnLoad_Click);
}
void btnLoad_Click(object sender, RoutedEventArgs e)
{
// 将进度列显示出来。
ProgressBar.Visibility = Visibility.Visible;
// 将文字 "下载中" 显示出来。
Label.Visibility = Visibility.Visible;
// 将进度列的 Value 属性值重新设定成 0 。
ProgressBar.Value = 0;
Label.Text = "下载中...";
ImageCanvas.Children.Clear();
if (_bitmapImage != null)
{
_bitmapImage.DownloadProgress -= new EventHandler<DownloadProgressEventArgs>(bitmapImage_DownloadProgress);
}
// 建立一个 BitmapImage 物件。
_bitmapImage = new BitmapImage();
// 指派 BitmapImage 物件的 DownloadProgress 事件处理常式。
_bitmapImage.DownloadProgress += new EventHandler<DownloadProgressEventArgs>(bitmapImage_DownloadProgress);
// 设定 BitmapImage 物件的照片来源。
_bitmapImage.UriSource = new Uri(txtUrl.Text, UriKind.Absolute);
// 动态建立一个 Image 控制项。
Image newImage = new Image() { Source = _bitmapImage };
// 设定 Image 控制项的属性。
newImage.Stretch = Stretch.UniformToFill;
newImage.Width = IMAGE_WIDTH;
newImage.Height = IMAGE_HEIGHT;
// 将动态建立的 Image 控制项加入画布中。
ImageCanvas.Children.Add(newImage);
}
void bitmapImage_DownloadProgress(object sender, DownloadProgressEventArgs e)
{
// 设定进度列的 Value 属性以便更新进度列。
ProgressBar.Value = e.Progress;
Label.Text = "下载中:" + e.Progress + "%";
if (e.Progress == 100)
{
ProgressBar.Visibility = Visibility.Collapsed;
Label.Visibility = Visibility.Collapsed;
}
}
}
}
前台代码
代码
<UserControl x:Class="SilverlightApplicationforCH9.CH9_DemoForm016"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="693" Height="617" Background="#FFFFFFFF">
<Border BorderThickness="5,5,5,5" CornerRadius="5,5,5,5" >
<Border.BorderBrush>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF000000"/>
<GradientStop Color="#FFE53838" Offset="1"/>
</LinearGradientBrush>
</Border.BorderBrush>
<Grid x:Name="LayoutRoot" Background="White" Margin="5,5,5,5">
<Grid.RowDefinitions>
<RowDefinition Height="40"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal">
<StackPanel.Background>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<GradientStop Color="#D9D9D9" Offset="0"/>
<GradientStop Color="#777777" Offset="0.5"/>
<GradientStop Color="#909090" Offset="0.5"/>
<GradientStop Color="#999999" Offset="1"/>
</LinearGradientBrush>
</StackPanel.Background>
<TextBlock Text="请输入照片的网址:" FontSize="14" Height="24" Foreground="#FF000000" />
<!-- 用来输入照片的网址 -->
<TextBox x:Name="txtUrl" FontSize="14" Width="457" Margin="6"/>
<Button x:Name="btnLoad" Content="下载照片" Margin="6" />
</StackPanel>
<!-- 此画布用来内含动态建立的 Image 控制项 -->
<Canvas x:Name="ImageCanvas" Grid.Row="1" Margin="5,5,5,5" Background="#FFE4E8D4" Width="640" Height="480"/>
<Grid Grid.Row="1" Margin="5,5,5,125">
<StackPanel VerticalAlignment="Center">
<TextBlock FontSize="12" Text="下载中..." Width="472" x:Name="Label" Margin="0, 0, 0, 10"/>
<!-- 进度列 -->
<ProgressBar Width="478" Height="20" x:Name="ProgressBar" Value="50" Foreground="#FF611B79"/>
</StackPanel>
</Grid>
</Grid>
</Border>
</UserControl>
<UserControl x:Class="SilverlightApplicationforCH9.CH9_DemoForm016"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="693" Height="617" Background="#FFFFFFFF">
<Border BorderThickness="5,5,5,5" CornerRadius="5,5,5,5" >
<Border.BorderBrush>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF000000"/>
<GradientStop Color="#FFE53838" Offset="1"/>
</LinearGradientBrush>
</Border.BorderBrush>
<Grid x:Name="LayoutRoot" Background="White" Margin="5,5,5,5">
<Grid.RowDefinitions>
<RowDefinition Height="40"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal">
<StackPanel.Background>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<GradientStop Color="#D9D9D9" Offset="0"/>
<GradientStop Color="#777777" Offset="0.5"/>
<GradientStop Color="#909090" Offset="0.5"/>
<GradientStop Color="#999999" Offset="1"/>
</LinearGradientBrush>
</StackPanel.Background>
<TextBlock Text="请输入照片的网址:" FontSize="14" Height="24" Foreground="#FF000000" />
<!-- 用来输入照片的网址 -->
<TextBox x:Name="txtUrl" FontSize="14" Width="457" Margin="6"/>
<Button x:Name="btnLoad" Content="下载照片" Margin="6" />
</StackPanel>
<!-- 此画布用来内含动态建立的 Image 控制项 -->
<Canvas x:Name="ImageCanvas" Grid.Row="1" Margin="5,5,5,5" Background="#FFE4E8D4" Width="640" Height="480"/>
<Grid Grid.Row="1" Margin="5,5,5,125">
<StackPanel VerticalAlignment="Center">
<TextBlock FontSize="12" Text="下载中..." Width="472" x:Name="Label" Margin="0, 0, 0, 10"/>
<!-- 进度列 -->
<ProgressBar Width="478" Height="20" x:Name="ProgressBar" Value="50" Foreground="#FF611B79"/>
</StackPanel>
</Grid>
</Grid>
</Border>
</UserControl>