【问题标题】:Windows Phone 7 Silverlight binding image from the IsolatedStorage来自独立存储的 Windows Phone 7 Silverlight 绑定图像
【发布时间】:2011-09-13 10:48:36
【问题描述】:

我需要找到将图像保存到 IsolatedStorage 并向他们展示 Silverlight (XAML) 的方法 重要提示:Silverlight 必须“自己”拍摄图像,我无法从后面的代码中设置图像 我之前尝试过很多解决方案。 最后的解决方案是绑定字节数组并将它们转换为图像 XAML

StackPanel Orientation="Horizontal" Margin="0,0,0,20">
                                <Image  Width="110" CacheMode="BitmapCache" Source="{Binding ThumbLocal,Converter={StaticResource imgConverter}}"  
                                        Margin="12,0,9,0"/>
                                <StackPanel Width="311">

后面的代码

public byte[] ThumbLocal
        {
            get;
            set;
        }


public class ByteImageConverter : IValueConverter
    {

           public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            MemoryStream memStream = new MemoryStream((byte[])value);
            memStream.Seek(0, SeekOrigin.Begin);
            BitmapImage thumbLocal = new BitmapImage();
            thumbLocal.SetSource(memStream);
            return thumbLocal;
        }
    }

在我将byte[] 保存到数据库并尝试检索之前,一切正常。 到目前为止,我可以看到唯一的选项将图像作为文件保存到 IsolatedStorage,然后检索并转换为 byte[]。 这是“智能”解决方案吗?

【问题讨论】:

    标签: silverlight windows-phone-7 isolatedstorage


    【解决方案1】:

    首先,创建这个转换器:

    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();
        }
    }
    

    其次,使用此转换器绑定到您的 byte[],即如果您使用的是 MVVM: 查看:

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

    您可以在控制(prop sn-p)类型字节[]中创建属性并从isostorage读取图像到字节数组,然后将属性值设置为它。 如果您还有其他问题,请随时问我。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多