C# 在转换器块执行中创建位图图像
我正在将图像源绑定到字节数组,并使用转换器从字节数组创建图像 这是我的XamlC# 在转换器块执行中创建位图图像,c#,winrt-xaml,windows-8.1,ivalueconverter,C#,Winrt Xaml,Windows 8.1,Ivalueconverter,我正在将图像源绑定到字节数组,并使用转换器从字节数组创建图像 这是我的Xaml <DataTemplate x:Key="OfferImageTemplate"> <Grid Margin="0,0,0,6"> <Image Source="{Binding ImageByteArray, Converter={StaticResource BinaryImageConverter}}"/> </
<DataTemplate x:Key="OfferImageTemplate">
<Grid Margin="0,0,0,6">
<Image Source="{Binding ImageByteArray, Converter={StaticResource BinaryImageConverter}}"/>
</Grid>
</DataTemplate>
问题是当执行btmimage.SetSource(a)
时,UI会完全阻塞,之后不会发生任何事情。
没有抛出异常
你知道为什么会这样吗
字节数组的内容是正确的,因为它们已在其他地方测试过
这是一个Windows 8.1应用商店应用程序。这有帮助吗?谢谢你的评论。问题是我不能在转换器中调用异步函数。如果不是这样的话,我会使用SetSourceAsync。似乎有一种方法可以在转换器中使用SetSourceAsync——看看这个线程:它似乎可以工作。我还发现了一个异步值转换器的实现。然而,我用解决方案解决了我的问题
public class BinaryImageConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
if (value != null && value is byte[])
{
byte[] bytes = value as byte[];
BitmapImage btmimage = new BitmapImage();
using (MemoryStream memstream = new MemoryStream(bytes))
{
using (var a = memstream.AsRandomAccessStream())
{
a.Seek(0);
btmimage.SetSource(a);
}
}
return btmimage;
}
return null;
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}