来自隔离存储的Windows Phone 7 Silverlight绑定映像
我需要找到将图像保存到IsolatedStorage的方法,并在Silverlight(XAML)中显示它们 重要提示:Silverlight必须拍摄“他自己”的图像,我无法从后面的代码设置图像 我以前尝试过很多解决方案。 最后一种解决方案是绑定字节数组并将其转换为映像 XAML 一切正常,直到我将来自隔离存储的Windows Phone 7 Silverlight绑定映像,silverlight,windows-phone-7,isolatedstorage,Silverlight,Windows Phone 7,Isolatedstorage,我需要找到将图像保存到IsolatedStorage的方法,并在Silverlight(XAML)中显示它们 重要提示:Silverlight必须拍摄“他自己”的图像,我无法从后面的代码设置图像 我以前尝试过很多解决方案。 最后一种解决方案是绑定字节数组并将其转换为映像 XAML 一切正常,直到我将字节[]保存到数据库并尝试检索为止。 到目前为止,我可以看到唯一的选项将图像另存为文件保存到IsolatedStorage,然后检索并转换为字节[]。 这是“聪明”的解决方案吗 首先,创建此转换器:
字节[]
保存到数据库并尝试检索为止。
到目前为止,我可以看到唯一的选项将图像另存为文件保存到IsolatedStorage,然后检索并转换为字节[]
。
这是“聪明”的解决方案吗 首先,创建此转换器:
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();
}
}
其次,使用此转换器绑定到字节[],即如果您使用的是MVVM:
视图:
您可以将contrlol(prop snippet)中的属性设置为byte[]类型,并从isostorage中读取图像到字节数组,然后将属性值设置为它。
如果你有更多的问题,请随时问我
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;
}
}
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();
}
}
<Image Source="{Binding IsolatedStorageImage, Converter={StaticResource BinaryToImageSourceConverter}}" x:Name="ScanImage"/>