Assalamu' alaikum。嗨,伙计们,当我将图像字节数据类型转换为Windows Phone中PictureBox上的图像时,我遇到了问题。它说" InvalidCastExpection未处理"。
这是背后的代码:
Namespace WP7_ClientApp
Public Class ImageConverter
Implements IValueConverter
Public Function ubah(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.Convert
Dim memStream As New MemoryStream(CType(value, Byte()))
memStream.Seek(0, SeekOrigin.Begin)
Dim gambar As New BitmapImage()
gambar.SetSource(memStream)
Return gambar
End Function
Public Function ConvertBack(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack
Throw New NotImplementedException()
End Function
End Class
End Namespace
然后这是XAML代码:
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<ListBox x:Name="daftar" ItemsSource="{Binding}" HorizontalAlignment="Left" Height="599" VerticalAlignment="Top" Width="456" Margin="12,10,0,0" Grid.Row="1">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" >
<Image Height="100" Source="{Binding gambar,Converter={StaticResource ImageConverter}}" HorizontalAlignment="Left" Margin="10,10,0,0" Name="Image1" Stretch="Fill" VerticalAlignment="Top" Width="100" />
<TextBlock Margin="10" Text="{Binding id}"/>
<TextBlock Margin="10" Text="{Binding namaproduk}"/>
<TextBlock Margin="10" Text="{Binding hargaproduk}"/>
<TextBlock Margin="10" Text="{Binding keterangan}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
然后这是错误截图:http://4.bp.blogspot.com/-qWB7oKLVE-s/UfpE0jhnhsI/AAAAAAAABuo/2eNvw2AmTEk/s1600/Capture.PNG
任何人都可以解决这个问题?我会很高兴,谢谢你。
*注意:gambar数据类型是Image,它有一个字节数据类型,我的表:http://4.bp.blogspot.com/-2ZMzi32TXsg/UfpJ5RqRG6I/AAAAAAAABu4/qe81rZm1pq0/s320/Capture.PNG
答案 0 :(得分:0)
您的对象是Binary类型,而您的转换器需要一个字节数组。不要将您的控件绑定到gambar
,而是绑定到gambar.Bytes
。