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 }"