【发布时间】:2013-08-01 20:53:59
【问题描述】:
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 数据类型是图像,它是字节数据类型,我的表:http://4.bp.blogspot.com/-2ZMzi32TXsg/UfpJ5RqRG6I/AAAAAAAABu4/qe81rZm1pq0/s320/Capture.PNG
【问题讨论】:
-
这只是意味着您的财产
gambar不是字节数组 -
根据您的屏幕截图,问题可能出在
CType(value, Byte())。可能值得检查value绝对是一个字节数组,并且CType对转换有效。 -
@KooKiz & Martin Parkin : 感谢您的回复,数据类型 gambar 是“图像”,它有一个字节数据类型,有什么问题吗?这是我的表格截图:4.bp.blogspot.com/-2ZMzi32TXsg/UfpJ5RqRG6I/AAAAAAAABu4/…
-
查看对象的类型,而不是数据库中列的类型。抛出异常时,将鼠标悬停在“值”上。
-
@KooKiz :你是这个意思吗? 1.bp.blogspot.com/-pdgo6iXrYCA/UfpLzlRU1kI/AAAAAAAABvI/…
标签: vb.net sql-server-2008 windows-phone-7 wcf-data-services vb.net-2010