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.