In many common small applications such as photo album/image switching, the server returns the URI of a group of images, and Silverlight uses WebClient for asynchronous loading. If you need to strictly control the loading sequence, you can use stack or queue for processing. Train of Thought: Do not load them all together. Load the first one first. In the asynchronous callback process, initiate another asynchronous request.
Recall that in Ajax development, there is a technology called "HTTP persistent connection" that continues to initiate the next asynchronous request when every Ajax asynchronous request is complete, in this way, the connection between the client and the server is maintained.
How similar they are! I once again confirmed my sentence: many times the technology is connected
KeyCode:
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>
///Sequentially, asynchronously loads a group of images in reverse order (by Yang Guo under the bodhi tree)Http://yjmyzz.cnblogs.com/)
/// </Summary>
Public Partial Class Mainpage: usercontrol
{
Stack < String > _ Imststack = New Stack < String > (); // To load data sequentially, replace it with queue <string>
WebClient _ WC = New WebClient ();
Public Mainpage ()
{
Initializecomponent ();
_ Imstack. Push ( " Gallery/scenes/1.jpg " );
_ Imstack. Push ( " Gallery/scenes/2.jpg " );
_ Imstack. Push ( " Gallery/scenes/3.jpg " );
_ Imstack. Push ( " Gallery/scenes/4.jpg " );
_ Imstack. Push ( " Gallery/scenes/5.jpg " );
_ Imstack. 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 (); // The key is to continue loading the next one (is it a bit of HTTP persistent connection in Ajax)
}
}
Private Void Btnload_click ( Object Sender, routedeventargs E)
{
LoadImage ();
}
Void LoadImage ()
{
If (_ Imgstack ! = Null && _ Imststack. 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, _ imstack. Pop ());
_ WC. openreadasync (_ imguri, _ itembase );
}
}
}
}
Source code: Http://files.cnblogs.com/yjmyzz/QueueLoad.rar