来自isolatedStorage的Windows

来自isolatedStorage的Windows

本文介绍了来自isolatedStorage的Windows Phone 7 Silverlight绑定图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 image from the code behindI have tried many solutions before.The very last solution is, to bind byte array and convert them to the ImageXAML

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[] 保存到数据库并尝试检索.到目前为止,我可以看到唯一的选项将图像另存为文件到隔离存储,然后检索并转换为 byte[].这是智能"解决方案吗?

Everything work till I save byte[] to the database and tried to retrieve.By now I can see the only option save image as file to the IsolatedStorage and then retrieve and covert to byte[].Is this "smart " solution?

推荐答案

首先,创建这个转换器:

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

第二,使用这个转换器绑定到你的 byte[],即如果你使用的是 MVVM:查看:

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

您可以在控制(道具片段)类型 byte[] 中创建属性并将图像从 isotorage 读取到字节数组,然后为其设置属性值.如果您有更多问题,请随时问我.

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.

这篇关于来自isolatedStorage的Windows Phone 7 Silverlight绑定图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 18:19