无图片尝试检索图像时显示

时间:2012-06-13 08:53:33

标签: c# wcf windows-phone-7

我在数据库中的图像没有显示在窗口电话上。 我只能在列表中获得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();
    }
  }

1 个答案:

答案 0 :(得分:0)

尝试应用转换器:

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