Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Binding to Image Path in Isolated Storage

0.00/5 (No votes)
19 Jun 2013 1  
Binding to Image Path in Isolated Storage

Binding Image Source to web address is not problem, it will work well... until you don't care about performance. The problem will appear if you want to cache images. Isolated storage is the only way to store downloaded files in Silverlight, no matter Browser or Windows Phone. It is not so difficult to download image file from the Internet and save to Isolated Storage, but how to use it in Silverlight Page?

Well, let's try to figure it out. First define the image converter:

public class IsoImageConverter : IValueConverter
{
    //Convert Data to Image when Loading Data
    public object Convert(object value, Type targetType, object parameter,
        System.Globalization.CultureInfo culture)
    {
        var bitmap = new BitmapImage();
        try
        {
            var path = (string)value;
            if (!String.IsNullOrEmpty(path))  
                {
                using (var file = LoadFile(path))
                {
                    bitmap.SetSource(file);
                }
            }
        }
        catch
        {
        }
        return bitmap;
    }

    private Stream LoadFile(string file)
    {
        using (var isoStore = IsolatedStorageFile.GetUserStoreForApplication())
        {
            return isoStore.OpenFile(file, FileMode.Open, FileAccess.Read);
        }
    }
    
    public object ConvertBack(object value, Type targetType, object parameter,
        System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Next, define the link in App.xaml for use in XAML code:

<textarea style="display:none;" 
class="originalCode"><local:IsoImageConverter x:Key="IsoImageCoverter"/>
</textarea>

And finally bind to string path to Isolated Storage image file:

Image Source="{Binding ImagePath, Converter=
{StaticResource IsoImageCoverter}}"/>

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here