silverlight里面使用大图片时,可能加载比较慢,加上进度显示。
1.建立用户控件LodingImage.xaml
Xaml 如下:
代码
<Grid x:Name="LayoutRoot" Background="White">
<Image x:Name="img"/>
<Image Source="default.png" x:Name="imgBack"/>
<TextBlock Text="正在加载 85%" x:Name="TxtLoading" HorizontalAlignment="Center" Foreground="Yellow" VerticalAlignment="Center" />
</Grid>
cs如下:
代码
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Net;
5 using System.Windows;
6 using System.Windows.Controls;
7 using System.Windows.Documents;
8 using System.Windows.Input;
9 using System.Windows.Media;
10 using System.Windows.Media.Animation;
11 using System.Windows.Shapes;
12 using System.Windows.Media.Imaging;
13
14 namespace WebImage
15 {
16 public partial class LodingImage : UserControl
17 {
18 /// <summary>
19 /// 图片地址
20 /// </summary>
21 public string Uri { get; set; }
22 public Stretch ImageStretch
23 {
24 get { return img.Stretch; }
25 set { img.Stretch = value; }
26 }
27
28 BitmapImage bitmapImage;
29
30 public LodingImage()
31 {
32 InitializeComponent();
33 this.Loaded += new RoutedEventHandler(LodingImage_Loaded);
34 }
35
36 void LodingImage_Loaded(object sender, RoutedEventArgs e)
37 {
38 bitmapImage = new BitmapImage();
39 img.Source = bitmapImage;
40 bitmapImage.UriSource = new Uri(Uri);
41 img.Stretch = Stretch.Fill;
42 bitmapImage.DownloadProgress += new EventHandler<DownloadProgressEventArgs>(img_DownloadProgress);
43 bitmapImage.ImageFailed += new EventHandler<ExceptionRoutedEventArgs>(img_ImageFailed);
44 bitmapImage.ImageOpened += new EventHandler<RoutedEventArgs>(img_ImageOpened);
45
46 }
47
48 void ReLoad()
49 {
50 bitmapImage = new BitmapImage();
51 }
52
53 void img_ImageOpened(object sender, RoutedEventArgs e)
54 {
55 imgBack.Visibility = TxtLoading.Visibility = System.Windows.Visibility.Collapsed;
56
57 }
58
59 void img_ImageFailed(object sender, ExceptionRoutedEventArgs e)
60 {
61 TxtLoading.Text = string.Format("加载失败");
62 }
63
64 void img_DownloadProgress(object sender, DownloadProgressEventArgs e)
65 {
66 TxtLoading.Text = string.Format("正在加载 {0}%", e.Progress);
67 }
68 }
69 }
70
2.调用如下
<WImage:LodingImage Uri="http://192.168.23.251:88/image/big.jpg" Width="350" Height="280" HorizontalAlignment="Left" ImageStretch="Uniform" />
参考了这位同学的做法
http://www.cnblogs.com/struggle-luan/archive/2009/01/04/1368471.html