Windows Phone 7 Silverlight binding image from the IsolatedStorage

前端 未结 1 1849
南笙
南笙 2020-12-18 17:29

I need to find the way to save images to the IsolatedStorage and show them I the Silverlight (XAML) Important: Silverlight have to take image “himself”, I cannot set the im

相关标签:
1条回答
  • 2020-12-18 18:02

    First, create this converter:

    public class BinaryToImageSourceConverter : IValueConverter
    {
    
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value != null && value is byte[])
            {
                var bytes = value as byte[];
                var stream = new MemoryStream(bytes);
                var image = new BitmapImage();
    
                image.SetSource(stream);
                stream.Close();
                return image;
            }
            return null;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    

    Second, bind to Your byte[] using this converter, i.e. if you are using MVVM: View:

    <Image Source="{Binding IsolatedStorageImage, Converter={StaticResource BinaryToImageSourceConverter}}" x:Name="ScanImage"/>
    

    You can make property in contrlol (prop snippet) type byte[] and read image to byte array from isostorage, then set value of property to it. If You got more questions, feel free to ask me.

    0 讨论(0)
提交回复
热议问题