silverlight中顺序/倒序异步加载多张图片

相册/图片切换广告等很多常用小应用中,服务器返回一组图片的uri,然后silverlight利用WebClient异步加载,如果要严格控制加载顺序的话,可以利用Stack(堆栈)或Queue(堆栈)处理,思路:不要全部一起加载,先加载第一个,在完成的异步回调过程中,继续发起一下次异步。

回想我们在ajax开发中,有一种技术叫"http长连接",在每一次ajax异步请求完成时,继续发起下一个异步请求,这样客户端与服务端的连接就一直保持下去了。

这二者多么相象!再次印证了我的那句话:技术很多时候一通百通 :)

关键代码:

silverlight中顺序/倒序异步加载多张图片using System;
silverlight中顺序/倒序异步加载多张图片
using System.Collections.Generic;
silverlight中顺序/倒序异步加载多张图片
using System.Net;
silverlight中顺序/倒序异步加载多张图片
using System.Windows;
silverlight中顺序/倒序异步加载多张图片
using System.Windows.Browser;
silverlight中顺序/倒序异步加载多张图片
using System.Windows.Controls;
silverlight中顺序/倒序异步加载多张图片
using System.Windows.Media.Imaging;
silverlight中顺序/倒序异步加载多张图片
using QueueLoad.controls;
silverlight中顺序/倒序异步加载多张图片
silverlight中顺序/倒序异步加载多张图片
namespace QueueLoad
silverlight中顺序/倒序异步加载多张图片silverlight中顺序/倒序异步加载多张图片
silverlight中顺序/倒序异步加载多张图片{
silverlight中顺序/倒序异步加载多张图片silverlight中顺序/倒序异步加载多张图片    
/**//// <summary>
silverlight中顺序/倒序异步加载多张图片    
/// 顺序,倒序异步加载一组图片(by 菩提树下的杨过 http://yjmyzz.cnblogs.com/)
silverlight中顺序/倒序异步加载多张图片    
/// </summary>

silverlight中顺序/倒序异步加载多张图片    public partial class MainPage : UserControl
silverlight中顺序/倒序异步加载多张图片silverlight中顺序/倒序异步加载多张图片    
silverlight中顺序/倒序异步加载多张图片{
silverlight中顺序/倒序异步加载多张图片        Stack
<string> _imgStack = new Stack<string>();//如要顺序加载,换成 Queue<string>
silverlight中顺序/倒序异步加载多张图片
       
silverlight中顺序/倒序异步加载多张图片        WebClient _wc 
= new WebClient();
silverlight中顺序/倒序异步加载多张图片
silverlight中顺序/倒序异步加载多张图片        
public MainPage()
silverlight中顺序/倒序异步加载多张图片silverlight中顺序/倒序异步加载多张图片        
silverlight中顺序/倒序异步加载多张图片{
silverlight中顺序/倒序异步加载多张图片            InitializeComponent();
silverlight中顺序/倒序异步加载多张图片
silverlight中顺序/倒序异步加载多张图片            _imgStack.Push(
"Gallery/Scenes/1.jpg");
silverlight中顺序/倒序异步加载多张图片            _imgStack.Push(
"Gallery/Scenes/2.jpg");
silverlight中顺序/倒序异步加载多张图片            _imgStack.Push(
"Gallery/Scenes/3.jpg");
silverlight中顺序/倒序异步加载多张图片            _imgStack.Push(
"Gallery/Scenes/4.jpg");
silverlight中顺序/倒序异步加载多张图片            _imgStack.Push(
"Gallery/Scenes/5.jpg");
silverlight中顺序/倒序异步加载多张图片            _imgStack.Push(
"Gallery/Scenes/6.jpg");
silverlight中顺序/倒序异步加载多张图片
silverlight中顺序/倒序异步加载多张图片            _wc.OpenReadCompleted 
+= _wc_OpenReadCompleted;
silverlight中顺序/倒序异步加载多张图片        }

silverlight中顺序/倒序异步加载多张图片
silverlight中顺序/倒序异步加载多张图片        
void _wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
silverlight中顺序/倒序异步加载多张图片silverlight中顺序/倒序异步加载多张图片        
silverlight中顺序/倒序异步加载多张图片{
silverlight中顺序/倒序异步加载多张图片            
if (e.Error == null)
silverlight中顺序/倒序异步加载多张图片silverlight中顺序/倒序异步加载多张图片            
silverlight中顺序/倒序异步加载多张图片{
silverlight中顺序/倒序异步加载多张图片                BitmapImage _bitMap 
= new BitmapImage();
silverlight中顺序/倒序异步加载多张图片                _bitMap.SetSource(e.Result);
silverlight中顺序/倒序异步加载多张图片
silverlight中顺序/倒序异步加载多张图片                ImageItemBase _itemBase 
= e.UserState as ImageItemBase;
silverlight中顺序/倒序异步加载多张图片
silverlight中顺序/倒序异步加载多张图片                _itemBase.img.Source 
= _bitMap;
silverlight中顺序/倒序异步加载多张图片                _itemBase.img.Visibility 
= Visibility.Visible;
silverlight中顺序/倒序异步加载多张图片                _itemBase.loading.Visibility 
= Visibility.Collapsed;
silverlight中顺序/倒序异步加载多张图片
silverlight中顺序/倒序异步加载多张图片                LoadImage();
//关键,继续加载下一个(是不是有点ajax中http长连接的意思,呵)
silverlight中顺序/倒序异步加载多张图片

silverlight中顺序/倒序异步加载多张图片            }

silverlight中顺序/倒序异步加载多张图片        }

silverlight中顺序/倒序异步加载多张图片
silverlight中顺序/倒序异步加载多张图片        
private void btnLoad_Click(object sender, RoutedEventArgs e)
silverlight中顺序/倒序异步加载多张图片silverlight中顺序/倒序异步加载多张图片        
silverlight中顺序/倒序异步加载多张图片{
silverlight中顺序/倒序异步加载多张图片            LoadImage();
silverlight中顺序/倒序异步加载多张图片        }

silverlight中顺序/倒序异步加载多张图片
silverlight中顺序/倒序异步加载多张图片
silverlight中顺序/倒序异步加载多张图片        
void LoadImage()
silverlight中顺序/倒序异步加载多张图片silverlight中顺序/倒序异步加载多张图片        
silverlight中顺序/倒序异步加载多张图片{
silverlight中顺序/倒序异步加载多张图片            
if (_imgStack != null && _imgStack.Count > 0)
silverlight中顺序/倒序异步加载多张图片silverlight中顺序/倒序异步加载多张图片            
silverlight中顺序/倒序异步加载多张图片{
silverlight中顺序/倒序异步加载多张图片                ImageItemBase _itemBase 
= new ImageItemBase();
silverlight中顺序/倒序异步加载多张图片                _itemBase.loading.Visibility 
= Visibility.Visible;
silverlight中顺序/倒序异步加载多张图片                _itemBase.img.Visibility 
= Visibility.Collapsed;
silverlight中顺序/倒序异步加载多张图片
silverlight中顺序/倒序异步加载多张图片                imgContainer.Children.Add(_itemBase);               
silverlight中顺序/倒序异步加载多张图片
silverlight中顺序/倒序异步加载多张图片                Uri _imgUri 
= new Uri(HtmlPage.Document.DocumentUri, _imgStack.Pop());
silverlight中顺序/倒序异步加载多张图片                _wc.OpenReadAsync(_imgUri, _itemBase);
silverlight中顺序/倒序异步加载多张图片            }

silverlight中顺序/倒序异步加载多张图片        }

silverlight中顺序/倒序异步加载多张图片    }

silverlight中顺序/倒序异步加载多张图片}

silverlight中顺序/倒序异步加载多张图片

源代码:http://files.cnblogs.com/yjmyzz/QueueLoad.rar

上一篇:山东智汇谷总裁高庆民:携手阿里赋能聊城产业升级。


下一篇:安装k8s 1.9.0 实践:问题集锦