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();
    }
  }
4

1 回答 1

0

尝试应用转换器:

Source="{Binding EmpImage, Converter={StaticResource imgConverter}}"
于 2012-06-13T08:55:59.333 回答