0

Assalamu'alaikum。嗨,大家好,我有一個問題,當我將圖像字節數據類型轉換爲Windows Phone中的PictureBox上的圖像。它說「InvalidCastExpection未處理」。無法將字節轉換爲圖像Windows Phone

這是後面的代碼:

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

任何人都可以解決這個問題?我會非常高興,謝謝。

*注:甘巴爾數據類型是圖片是有一個字節的數據類型,我的表:http://4.bp.blogspot.com/-2ZMzi32TXsg/UfpJ5RqRG6I/AAAAAAAABu4/qe81rZm1pq0/s320/Capture.PNG

+0

它只是意味着你的財產'gambar'不是一個字節數組 –

+0

根據你的屏幕截圖,問題很可能是'CType(value,Byte())'。可能值得檢查一下'value'肯定是一個字節數組,'CType'對於轉換是有效的。 –

+0

@KooKiz&Martin Parkin:感謝您的回覆,數據類型gambar是「Image」,它有一個字節的數據類型,出了什麼問題?這是我的表格截圖:http://4.bp.blogspot.com/-2ZMzi32TXsg/UfpJ5RqRG6I/AAAAAAAABu4/qe81rZm1pq0/s320/Capture.PNG –

回答

0

你的對象是二進制類型,而你的轉換器期待一個字節數組。不要將您的控件綁定到gambar,而要綁定到gambar.Bytes

+0

謝謝先生。 :) –