C# Windows 8 XAML是否将字节[]绑定到图像控件?
我试图在图像控件中绑定字节[],但转换器中存在一些问题。请让我知道如何修复此问题? 我在第C# Windows 8 XAML是否将字节[]绑定到图像控件?,c#,.net,windows-8,windows-runtime,winrt-xaml,C#,.net,Windows 8,Windows Runtime,Winrt Xaml,我试图在图像控件中绑定字节[],但转换器中存在一些问题。请让我知道如何修复此问题? 我在第行stream.WriteAsync(bytesArray.AsBuffer())上遇到错误因为字节[]中没有AsBuffer函数。我该如何解决这个问题 代码: XAMl代码: <Image Source="{Binding Path=OnlineBooksDetail[0].ImageSource, Converter={StaticResource ByteToBitmapImageConvert
行stream.WriteAsync(bytesArray.AsBuffer())上遇到错误代码>因为字节[]中没有AsBuffer函数。我该如何解决这个问题
代码:
XAMl代码:
<Image Source="{Binding Path=OnlineBooksDetail[0].ImageSource, Converter={StaticResource ByteToBitmapImageConverter}}" Width="407" Height="542">
</Image>
我可以通过如下所述创建转换器来解决此问题。下面的程序正在运行。如果我能改进它,一定要告诉我
代码:
位图图像
/// <summary>
/// Converts the specified value.
/// </summary>
/// <param name="value">The value.</param>
/// <param name="targetType">Type of the target.</param>
/// <param name="parameter">The parameter.</param>
/// <param name="language">The language.</param>
/// <returns></returns>
public object Convert(object value, Type targetType, object parameter, string language)
{
byte[] bytesArray = null;
if (value != null && value is byte[] && (value as byte[]).Length > 0)
{
bytesArray = value as byte[];
}
else
{
//TODO: Add default Image here
}
using (MemoryRandomAccessStream memoryStream = new MemoryRandomAccessStream(bytesArray))
{
this.image = new BitmapImage();
this.SetImageSource(memoryStream);
return image;
}
}
/// <summary>
/// Sets the image source.
/// </summary>
/// <param name="memoryStream">The memory stream.</param>
/// <exception cref="System.NotImplementedException"></exception>
private async void SetImageSource(MemoryRandomAccessStream memoryStream)
{
await this.image.SetSourceAsync(memoryStream);
}
//
///转换指定的值。
///
///价值。
///目标的类型。
///参数。
///语言。
///
公共对象转换(对象值、类型targetType、对象参数、字符串语言)
{
字节[]字节数组=null;
如果(值!=null&&值为字节[]&&(值为字节[])。长度>0)
{
bytesArray=字节[]形式的值;
}
其他的
{
//TODO:在此处添加默认图像
}
使用(MemoryRandomAccessStream memoryStream=新的MemoryRandomAccessStream(字节数组))
{
this.image=新的BitmapImage();
此.SetImageSource(memoryStream);
返回图像;
}
}
///
///设置图像源。
///
///内存流。
///
专用异步void SetImageSource(MemoryRandomAccessStream memoryStream)
{
等待这个.image.SetSourceAsync(memoryStream);
}
在WPF中,您可以将字节[]
直接绑定到源代码
,这在Windows运行时不起作用吗?好的,这就是我需要转换器的原因。你能提供一些帮助更新我写的一个或其他一些方法来实现这一点吗?我不熟悉WindowsRuntime,也不知道里面有什么,它是否有MemoryStream
是的。我已经将字节[]转换为MemeoryStream,比如:MemoryStream MemoryStream=newmemoryStream(bytesArray)。但现在的挑战是如何将内存流写入BitmapImage?
/// <summary>
/// Converts the specified value.
/// </summary>
/// <param name="value">The value.</param>
/// <param name="targetType">Type of the target.</param>
/// <param name="parameter">The parameter.</param>
/// <param name="language">The language.</param>
/// <returns></returns>
public object Convert(object value, Type targetType, object parameter, string language)
{
byte[] bytesArray = null;
if (value != null && value is byte[] && (value as byte[]).Length > 0)
{
bytesArray = value as byte[];
}
else
{
//TODO: Add default Image here
}
using (MemoryRandomAccessStream memoryStream = new MemoryRandomAccessStream(bytesArray))
{
this.image = new BitmapImage();
this.SetImageSource(memoryStream);
return image;
}
}
/// <summary>
/// Sets the image source.
/// </summary>
/// <param name="memoryStream">The memory stream.</param>
/// <exception cref="System.NotImplementedException"></exception>
private async void SetImageSource(MemoryRandomAccessStream memoryStream)
{
await this.image.SetSourceAsync(memoryStream);
}