Windows Phone 7 (WP7) Development image Cache

Source: Internet
Author: User

Recently, I am working on a WP7 client, which involves retrieving images from the Internet, while the wireless network of mobile phones is actually very slow (even if it is Unicom's 3G, I don't feel how fast ), therefore, caching is essential.

In fact, it is easy to cache on WP7, and the code is directly written:

& Lt; Image Height = "150" canvas. left = "8" canvas. top = "8" width = "150" Source = "{binding picid, converter = {staticresource imageconverter}, mode = oneway}"/>

The image control is mainly used to set the source attribute, bind the image ID, and set converter.

 

Public class imageconverter: ivalueconverter
{

Public object convert (object value, type targettype, object parameter, system. Globalization. cultureinfo Culture)
{

Imagesource source = imagecache. getimage (value. tostring ());

Return source;
}

Public object convertback (object value, type targettype, object parameter, system. Globalization. cultureinfo Culture)
{
Return "";
}

}

Converter does not actually have much content. It mainly transmits the picid to the cache class. below is the cache code:

 

Public class imagecache

{
Public static dictionary <string, imagesource> imagesources = new dictionary <string, imagesource> ();

Static imagecache ()
{
Imagesources. Add ("", new bitmapimage (New uri (staticresource. pathnoimage, urikind. Relative )));
}

Public static imagesource getimage (string imageid)
{
If (! Imagesources. containskey (imageid ))
{
Imagesource source = new bitmapimage (New uri (staticresource. urlpicture + imageid ));
Imagesources. Add (imageid, source );
}

Return imagesources [imageid];
}
}


My cache only uses a dictionary <string, imagesource> in the memory for caching. Of course, you are interested in using an isolated bucket to store images.

 

 

Another note: In mango (I haven't tried it in the previous version), it is not necessary to write HTTP requests to retrieve images from the network.

Imagesource source = new bitmapimage (New uri ("HTTP address of the image "));

You can.

 

Related Article

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.