Silverlight 图片进度化加载小工具类
快速将加载图片的过程可视化,后台化.
今天的小东西,在Silverlight.net上回答问题时候写的,共享了。
---------------------------------------------------------ImageLoader Class---------------------------------------------------------
//Created by Struggle-luan //Thanks for using~ using System; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Ink; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using System.Collections.Generic; using System.Windows.Media.Imaging; namespace Silverlight_ImageLoader { public class ImageLoader { //FDS: private int ID_Count; public int TaskCount { get { return ID_Count; } } private double C_Progress; public double TaskProgress { get { return C_Progress / ID_Count; } } private List<ImageData> OperatingData; private Grid G_Temp; private Panel Container; private Dictionary<BitmapImage,int> ProgressCounter; private bool AvoidDoubleFinishes = false; //DELES: public delegate void D_ProgressChanged(ImageLoader Sender, double Progress); public delegate void D_TaskAllFinished(ImageLoader Sender,List<Image> DataOutPut); //PUB_EVENTS: public event D_ProgressChanged ProgressChanged; public event D_TaskAllFinished TaskFinished; //P_FUNCT" public void LoadImages(List<ImageData> RequestDataList,Panel VisibleContainer) { AvoidDoubleFinishes = false; ProgressCounter = new Dictionary<BitmapImage,int>(); Container = VisibleContainer; OperatingData = RequestDataList; C_Progress = 0; ID_Count = RequestDataList.Count; G_Temp = new Grid(); G_Temp.Height = 0; G_Temp.Width = 0; foreach (ImageData ID in OperatingData) { BitmapImage BI = new BitmapImage(); ProgressCounter.Add(BI, 0); BI.DownloadProgress += new EventHandler<DownloadProgressEventArgs>(BI_DownloadProgress); BI.UriSource = ID.TargetURI; ID.TargetImagePointer.Source = BI; G_Temp.Children.Add(ID.TargetImagePointer); } Container.Children.Add(G_Temp); } //INNER_EVENTS: void BI_DownloadProgress(object sender, DownloadProgressEventArgs e) { ProgressCounter[sender as BitmapImage] = e.Progress; int Temp = 0; foreach (int i in ProgressCounter.Values) { Temp += i; } C_Progress = Temp; if (TaskProgress == 100 && (!AvoidDoubleFinishes)) { List<Image> Ready = new List<Image>(); foreach (ImageData ID in OperatingData) { G_Temp.Children.Remove(ID.TargetImagePointer); Ready.Add(ID.TargetImagePointer); } TaskFinished.Invoke(this,Ready); AvoidDoubleFinishes = true; Container.Children.Remove(G_Temp); } else { ProgressChanged.Invoke(this,TaskProgress); } } } public class ImageData { public Uri TargetURI { get; set; } public Image TargetImagePointer { get; set; } } }
---------------------------------------------------------Page.xaml------------------------------------------------------------------------------------
<UserControl x:Class="Silverlight_ImageLoader.Page" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" > <Grid x:Name="LayoutRoot" Background="White"> <Grid.RowDefinitions> <RowDefinition/> <RowDefinition Height="30"/> </Grid.RowDefinitions> <Button x:Name="TEST" Grid.Row="1" Content="Let's do it" Click="Test"/> <ScrollViewer VerticalScrollBarVisibility="Auto"> <StackPanel x:Name="StackMain"/> </ScrollViewer> </Grid> </UserControl>
----------------------------------------------------Page.xaml.cs---------------------------------------------------------
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 Silverlight_ImageLoader { public partial class Page : UserControl { public Page() { InitializeComponent(); PB = new ProgressBar(); LayoutRoot.Children.Add(PB); PB.Visibility = Visibility.Collapsed; PB.SetValue(Grid.RowProperty, 1); } ProgressBar PB; private void Test(object sender, RoutedEventArgs e) { PB.Maximum = 100; PB.Value = 0; PB.Visibility = Visibility.Visible; List<ImageData> LD = new List<ImageData>(); LD.Add(new ImageData { TargetURI = new Uri("http://www.geobabble.org/~hnw/neon/withindomainrep2/withindomainrep2.neonIII.clustdist2.medium.png"), TargetImagePointer = new Image() }); LD.Add(new ImageData { TargetURI = new Uri("http://www.astray.com/static/earth.png"), TargetImagePointer = new Image() }); ImageLoader IL = new ImageLoader(); IL.ProgressChanged += new ImageLoader.D_ProgressChanged(IL_ProgressChanged); IL.TaskFinished += new ImageLoader.D_TaskAllFinished(IL_TaskFinished); IL.LoadImages(LD, LayoutRoot); } void IL_TaskFinished(ImageLoader Sender, List<Image> DataOutPut) { PB.Visibility = Visibility.Collapsed; foreach (Image I in DataOutPut) { StackMain.Children.Add(I); } } void IL_ProgressChanged(ImageLoader Sender, double Progress) { PB.Value = Progress; } void B_DownloadProgress(object sender, DownloadProgressEventArgs e) { } } }
以上就是所有的代码了。。。
至少解决了我自己的问题,弥补了Silverlight图片加载时候一片白,死机状的情景。。
下面的是Sample下载。。
觉得稍微有点作用的就拿这个类爽一爽。。
https://files.cnblogs.com/struggle-luan/SilverlightImgLoader.zip
(例子里面图片加载的大小大概是10mb左右。。Google上搜索HugePicture...)
小玩意,不值一提。。各位高手见笑了