来自isolatedStorage的Windows Phone 7 Silverlight绑定图像

编程入门 行业动态 更新时间:2024-10-28 00:27:49
本文介绍了来自isolatedStorage的Windows Phone 7 Silverlight绑定图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我需要找到将图像保存到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 behind I have tried many solutions before. The very last solution is, to bind byte array and convert them to the Image 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[] 保存到数据库并尝试检索.到目前为止,我可以看到唯一的选项将图像另存为文件到隔离存储,然后检索并转换为 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绑定图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-25 15:40:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1118560.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:绑定   图像   Windows   isolatedStorage   Silverlight

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!