silverlight中顺序/倒序异步加载多张图片
相册/图片切换广告等很多常用小应用中,服务器返回一组图片的uri,然后silverlight利用WebClient异步加载,如果要严格控制加载顺序的话,可以利用Stack(堆栈)或Queue(堆栈)处理,思路:不要全部一起加载,先加载第一个,在完成的异步回调过程中,继续发起一下次异步。
回想我们在ajax开发中,有一种技术叫"http长连接",在每一次ajax异步请求完成时,继续发起下一个异步请求,这样客户端与服务端的连接就一直保持下去了。
这二者多么相象!再次印证了我的那句话:技术很多时候一通百通 :)
关键代码:
using System;
using System.Collections.Generic;
using System.Net;
using System.Windows;
using System.Windows.Browser;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using QueueLoad.controls;
namespace QueueLoad
{
/// <summary>
/// 顺序,倒序异步加载一组图片(by 菩提树下的杨过 http://yjmyzz.cnblogs.com/)
/// </summary>
public partial class MainPage : UserControl
{
Stack<string> _imgStack = new Stack<string>();//如要顺序加载,换成 Queue<string>
WebClient _wc = new WebClient();
public MainPage()
{
InitializeComponent();
_imgStack.Push("Gallery/Scenes/1.jpg");
_imgStack.Push("Gallery/Scenes/2.jpg");
_imgStack.Push("Gallery/Scenes/3.jpg");
_imgStack.Push("Gallery/Scenes/4.jpg");
_imgStack.Push("Gallery/Scenes/5.jpg");
_imgStack.Push("Gallery/Scenes/6.jpg");
_wc.OpenReadCompleted += _wc_OpenReadCompleted;
}
void _wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
if (e.Error == null)
{
BitmapImage _bitMap = new BitmapImage();
_bitMap.SetSource(e.Result);
ImageItemBase _itemBase = e.UserState as ImageItemBase;
_itemBase.img.Source = _bitMap;
_itemBase.img.Visibility = Visibility.Visible;
_itemBase.loading.Visibility = Visibility.Collapsed;
LoadImage();//关键,继续加载下一个(是不是有点ajax中http长连接的意思,呵)
}
}
private void btnLoad_Click(object sender, RoutedEventArgs e)
{
LoadImage();
}
void LoadImage()
{
if (_imgStack != null && _imgStack.Count > 0)
{
ImageItemBase _itemBase = new ImageItemBase();
_itemBase.loading.Visibility = Visibility.Visible;
_itemBase.img.Visibility = Visibility.Collapsed;
imgContainer.Children.Add(_itemBase);
Uri _imgUri = new Uri(HtmlPage.Document.DocumentUri, _imgStack.Pop());
_wc.OpenReadAsync(_imgUri, _itemBase);
}
}
}
}
using System.Collections.Generic;
using System.Net;
using System.Windows;
using System.Windows.Browser;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using QueueLoad.controls;
namespace QueueLoad
{
/// <summary>
/// 顺序,倒序异步加载一组图片(by 菩提树下的杨过 http://yjmyzz.cnblogs.com/)
/// </summary>
public partial class MainPage : UserControl
{
Stack<string> _imgStack = new Stack<string>();//如要顺序加载,换成 Queue<string>
WebClient _wc = new WebClient();
public MainPage()
{
InitializeComponent();
_imgStack.Push("Gallery/Scenes/1.jpg");
_imgStack.Push("Gallery/Scenes/2.jpg");
_imgStack.Push("Gallery/Scenes/3.jpg");
_imgStack.Push("Gallery/Scenes/4.jpg");
_imgStack.Push("Gallery/Scenes/5.jpg");
_imgStack.Push("Gallery/Scenes/6.jpg");
_wc.OpenReadCompleted += _wc_OpenReadCompleted;
}
void _wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
if (e.Error == null)
{
BitmapImage _bitMap = new BitmapImage();
_bitMap.SetSource(e.Result);
ImageItemBase _itemBase = e.UserState as ImageItemBase;
_itemBase.img.Source = _bitMap;
_itemBase.img.Visibility = Visibility.Visible;
_itemBase.loading.Visibility = Visibility.Collapsed;
LoadImage();//关键,继续加载下一个(是不是有点ajax中http长连接的意思,呵)
}
}
private void btnLoad_Click(object sender, RoutedEventArgs e)
{
LoadImage();
}
void LoadImage()
{
if (_imgStack != null && _imgStack.Count > 0)
{
ImageItemBase _itemBase = new ImageItemBase();
_itemBase.loading.Visibility = Visibility.Visible;
_itemBase.img.Visibility = Visibility.Collapsed;
imgContainer.Children.Add(_itemBase);
Uri _imgUri = new Uri(HtmlPage.Document.DocumentUri, _imgStack.Pop());
_wc.OpenReadAsync(_imgUri, _itemBase);
}
}
}
}
作者:菩提树下的杨过
出处:http://yjmyzz.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
出处:http://yjmyzz.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。