WP7 images are bound from independent storage

Source: Internet
Author: User

 
Requirement: You need to bind an image to a ListBox. Set Source as needed, which may be files in isolatedstorage or xap.
 
CodeAs follows:
 
The first is a converter code, which contains How To Read File streams from isolatedstorage and xap.
 
 
 Public   Class Stringtoimagesource: ivalueconverter { # Region Ivalueconverter members Public   Object Convert (Object   Value , Type targettype, Object Parameter, system. Globalization. cultureinfo culture ){ Try {Bitmapimage ret = New Bitmapimage (); String Filepath = ( String ) Value ; Using (Isolatedstoragefile ISO = isolatedstoragefile. getuserstoreforapplication ()){ If (ISO. fileexists (filepath )){ // Use the files in the isolated storage                     Using (VAR source = ISO. openfile (filepath, filemode. Open, fileaccess. Read) {ret. setsource (source );}} Else { // Use the resource file (set buildaction to content) Uri uri = New Uri (filepath, urikind. relativeorabsolute); streamresourceinfo Sri = application. getresourcestream (URI); ret. setsource (SRI. Stream );} Return RET ;}} Catch (Exception ){ Return   Null ;// If an exception occurs, leave the image position blank. }} Public   Object Convertback ( Object   Value , Type targettype, Object Parameter, system. Globalization. cultureinfo culture ){ Return Dependencyproperty. unsetvalue ;} # Endregion }

 

The XAML binding is written as follows:

Source = "{binding IMG, converter = {staticresource stringtoimagesource }"

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.