2012-06-13 97 views
0

我的數據庫圖像未顯示在窗口電話上。 我只能在列表中獲得EmpNo,但圖像沒有顯示在我的列表框中。 我的編碼有問題嗎?無圖片嘗試檢索圖片時顯示

我在MainPage.xaml中編碼是

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> 
     <Grid.Resources> 
      <src:ImageConverter x:Key="imgConverter"/> 
     </Grid.Resources> 
     <ListBox Height="650" HorizontalAlignment="Left" Margin="11,17,0,0" Name="listBox1" VerticalAlignment="Top" Width="434" > 
      <ListBox.ItemTemplate> 
       <DataTemplate> 
        <StackPanel Orientation="Horizontal"> 
         <TextBlock Text="{Binding EmpNo}"/> 
         <Image Grid.Column="1" Grid.Row="4" Height="136" HorizontalAlignment="Left" 
       Margin="16,16,0,0" Name="imgEmp" Stretch="Fill" 
       VerticalAlignment="Top" Width="200" 
       Source="{Binding EmpImage}"/> 
        </StackPanel> 
       </DataTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox> 
</Grid> 

和我在MainPage.xaml.cs中編碼是

public partial class MainPage : PhoneApplicationPage 
{ 
    public MainPage() 
    { 
     InitializeComponent(); 
     Service1Client svc = new Service1Client(); 
     svc.GetAllModelsCompleted += new EventHandler<GetAllModelsCompletedEventArgs>(svc_GetAllModelsCompleted); 
     svc.GetAllModelsAsync(); 
    } 

    void svc_GetAllModelsCompleted(object sender, GetAllModelsCompletedEventArgs e) 
    { 
     listBox1.ItemsSource = e.Result; 
    } 
} 
public class ImageConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     var memStream = new MemoryStream((byte[])value); 
     memStream.Seek(0, SeekOrigin.Begin); 
     var empImage = new BitmapImage(); 
     empImage.CreateOptions = BitmapCreateOptions.None; 
     empImage.SetSource(memStream); 
     return empImage; 
    } 
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
    } 

回答

0

嘗試應用轉換器:

Source="{Binding EmpImage, Converter={StaticResource imgConverter}}" 
+0

日Thnx。但現在有另一個錯誤invalidCastException未處理var memStream = new MemoryStream((byte [])value); –

+0

@Brownieyeo那麼你的'value'不能被轉換成字節數組。 – CodeCaster

+0

但我曾嘗試使用在其他項目的價值和它的作品.. –