我的 Windows Phone 应用程序有问题。当我从 Web 服务获取照片时,我想将其作为图像显示在页面上。 Web 服务返回一个 byte[] 作为图像数据。
Dispatcher.BeginInvoke(() =>
{
tempImage = new BitmapImage();
globalWrapper = (PhotoWrapper)JsonConvert.DeserializeObject(
response.Content, typeof(PhotoWrapper));
tempImage.SetSource(new MemoryStream(globalWrapper.PictureBinary, 0,
globalWrapper.PictureBinary.Length));
globalWrapper.ImageSource = tempImage;
PictureList.Items.Add(globalWrapper);
});
PictureList 是一个 ListBox,定义为:
<ListBox Name="PictureList" ItemsSource="{Binding}" Margin="0,0,0,0">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<Button Click="details_Click">
<Button.Content>
<Image Source="{Binding ImageSource}"></Image>
</Button.Content>
</Button>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
现在,我的问题是,如何从 Web 服务接收字节 [] 作为 JSON 并将其显示在页面上?我觉得我在这里很接近,但缺少一些相当基本的东西。
请您参考如下方法:
如果您确定 byte[] 数据有效,则可能与 CacheOption 有关BitmapUmage 的属性。此属性控制何时将数据实际从流加载到位图中。默认为 OnDemand,它仅在显示图像时从流中加载数据。您可能想尝试使用 OnLoad 选项,它会立即加载它,从而允许您关闭流。
Dispatcher.BeginInvoke(() =>
{
globalWrapper = (PhotoWrapper)JsonConvert.DeserializeObject(
response.Content, typeof(PhotoWrapper));
tempImage = new BitmapImage();
tempImage.BeginInit();
tempImage.CacheOption = BitmapCacheOption.OnLoad;
tempImage.SetSource(new MemoryStream(globalWrapper.PictureBinary, 0,
globalWrapper.PictureBinary.Length));
tempImage.EndInit();
globalWrapper.ImageSource = tempImage;
PictureList.Items.Add(globalWrapper);
});
