Load multiple images asynchronously in order/Reverse Order in Silverlight

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.