【发布时间】:2013-02-06 06:07:28
【问题描述】:
我正在尝试将 byte[] 绑定到图像控件中,但转换器中存在一些问题。请让我知道我该如何解决这个问题?
我在stream.WriteAsync(bytesArray.AsBuffer()); 线上遇到错误,因为 byte[] 中没有 AsBuffer 函数。我该如何解决这个问题?
代码:
public object Convert(object value, Type targetType, object parameter, string language)
{
byte[] bytesArray;
if (value != null && value is byte[] && (value as byte[]).Length > 0)
{
bytesArray = value as byte[];
}
else
{
//TODO: Add default Image here
}
using (InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream())
{
BitmapImage image = new BitmapImage();
stream.WriteAsync(bytesArray.AsBuffer());
stream.Seek(0);
image.SetSource(stream);
return image;
}
}
XAMl 代码:
<Image Source="{Binding Path=OnlineBooksDetail[0].ImageSource, Converter={StaticResource ByteToBitmapImageConverter}}" Width="407" Height="542">
</Image>
【问题讨论】:
-
在 WPF 中你可以将
byte[]直接绑定到Source,这在 WindowsRuntime 中不起作用吗? -
好的,这就是我需要转换器的原因。你能提供一些帮助更新我写的或其他方式来实现这一点吗?
-
我不熟悉WindowsRuntime,也不知道里面有什么,有
MemoryStream -
是的。我已将 byte[] 转换为 MemeoryStream,例如: MemoryStream memoryStream = new MemoryStream (bytesArray)。但现在的挑战是如何将内存流写入 BitmapImage?
标签: c# .net windows-8 windows-runtime winrt-xaml