来自IsolatedStorage的Windows Phone 7 Silverlight绑定映像
发布时间:2020-12-13 20:31:36 所属栏目:Windows 来源:网络整理
导读:我需要找到将图像保存到IsolatedStorage的方法并向他们展示Silverlight(XAML) 重要:Silverlight必须拍摄“自己”的图像,我无法从背后的代码中设置图像 我之前尝试了很多解决方案. 最后一个解决方案是绑定字节数组并将它们转换为Image XAML StackPanel Orien
我需要找到将图像保存到IsolatedStorage的方法并向他们展示Silverlight(XAML)
重要:Silverlight必须拍摄“自己”的图像,我无法从背后的代码中设置图像 我之前尝试了很多解决方案. 最后一个解决方案是绑定字节数组并将它们转换为Image XAML StackPanel Orientation="Horizontal" Margin="0,20"> <Image Width="110" CacheMode="BitmapCache" Source="{Binding ThumbLocal,Converter={StaticResource imgConverter}}" Margin="12,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 []保存到数据库并尝试检索.
首先,创建此转换器:
public class BinaryToImageSourceConverter : IValueConverter { public object Convert(object value,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,System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } 其次,使用此转换器绑定到您的byte [],即如果您使用的是MVVM: <Image Source="{Binding IsolatedStorageImage,Converter={StaticResource BinaryToImageSourceConverter}}" x:Name="ScanImage"/> 您可以在contrlol(prop snippet)类型byte []中创建属性,并从isostorage读取图像到字节数组,然后将属性值设置为它.如果您有更多问题,请随时问我. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |